Archived
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:
1 parent
543cba7416
commit
fad4ba7f1a
63 files changed
+6162
-169
No files matched your search
@@ -25,7 +25,9 @@
|
||||
#include "lfm2_golden.h"
|
||||
#include "mm/resample.h"
|
||||
#include "mm_golden.h"
|
||||
#include "rad_builder.h"
|
||||
#include "rad_plugin.h"
|
||||
#include "uocr_golden.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -1309,4 +1311,348 @@ TEST(a_resampled_stream_is_its_tone_whether_pushed_whole_or_in_pieces) {
|
||||
CHECK(!mm::Resampler::takes(23999));
|
||||
}
|
||||
|
||||
|
||||
/* ================================================================== Unlimited-OCR
|
||||
*
|
||||
* The checkpoint's two picture modes, from its own remote code (tests/mkuocrgolden.py): the tile
|
||||
* grid dynamic_preprocess chooses (its ratio set sorted by area, ties in the set's order), the
|
||||
* placeholders that replace the picture (infer()'s global view plus crops; infer_multi()'s page),
|
||||
* and the pixels of the global view and of the tile mosaic, in the order Item::patch_raster
|
||||
* stages them: row by row, each patch (channel, y, x).
|
||||
*/
|
||||
|
||||
/* The checkpoint's processor configuration, as a converted container states it. */
|
||||
VisionConfig uocr_cfg() {
|
||||
VisionConfig c;
|
||||
c.family = Family::UnlimitedOcr;
|
||||
c.patch = kUocrPatch;
|
||||
c.temporal = 1;
|
||||
c.merge = kUocrDownsample;
|
||||
c.downsample = kUocrDownsample;
|
||||
c.base_size = kUocrBaseSize;
|
||||
c.image_size = kUocrBaseSize;
|
||||
c.tile = kUocrTile;
|
||||
c.min_tiles = kUocrMinTiles;
|
||||
c.max_tiles = kUocrMaxTiles;
|
||||
c.crop_mode = kUocrCropMode;
|
||||
c.tok_image = kUocrImageToken;
|
||||
c.max_patches = 16384; /* the server's --mm-max-patches default */
|
||||
const int64_t f2 = (int64_t)c.factor() * c.factor();
|
||||
c.image_min_pixels = c.video_min_pixels = f2;
|
||||
c.image_max_pixels = c.video_max_pixels = (int64_t)c.base_size * c.base_size;
|
||||
c.min_frames = 1;
|
||||
return c;
|
||||
}
|
||||
|
||||
int32_t uocr_marker(const std::string& s) { return s == "<image>" ? kUocrImageToken : -1; }
|
||||
|
||||
/* A segment's patches in raster order, its first `real_w` patches a row: the tensor the reference
|
||||
* holds for that view, patch by patch. */
|
||||
std::vector<uint16_t> segment_pixels(const Item& it, int32_t s, int32_t real_w) {
|
||||
const int64_t pd = it.patch_dim;
|
||||
const int32_t gh = it.seg_h(s), gw = it.seg_w(s);
|
||||
const int64_t p0 = it.patch_at(s);
|
||||
std::vector<uint16_t> out;
|
||||
out.reserve((size_t)gh * (size_t)real_w * (size_t)pd);
|
||||
for (int32_t py = 0; py < gh; ++py)
|
||||
for (int32_t px = 0; px < real_w; ++px) {
|
||||
const uint16_t* p = it.pixels.data() + (p0 + (int64_t)py * gw + px) * pd;
|
||||
out.insert(out.end(), p, p + pd);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/* The container's model_type selects the family; a key it does not carry takes the reference's
|
||||
* default, so a container converted before the tower landed still states a processor. */
|
||||
TEST(unlimitedocr_configures_itself_from_the_container) {
|
||||
const char* keys[] = { "model_type", "preprocessor.crop_to_patches", "preprocessor.max_patches",
|
||||
"preprocessor.min_patches", "preprocessor.resample",
|
||||
"preprocessor.size.height", "preprocessor.size.width",
|
||||
"preprocessor.tile_size", "processor.downsample_ratio",
|
||||
"processor.image_token", "processor.patch_size" };
|
||||
const char* vals[] = { "unlimited_ocr", "true", "32", "2", "3", "1024", "1024", "640", "4",
|
||||
"<image>", "16" };
|
||||
RadModelMeta m{};
|
||||
m.n_kv = (int)(sizeof keys / sizeof keys[0]);
|
||||
m.kv_key = keys;
|
||||
m.kv_val = vals;
|
||||
VisionConfig c;
|
||||
std::string why;
|
||||
REQUIRE_EQ(c.from_meta(m, &why), RAD_OK);
|
||||
CHECK(c.family == Family::UnlimitedOcr);
|
||||
CHECK_EQ(c.patch, 16);
|
||||
CHECK_EQ(c.downsample, 4);
|
||||
CHECK_EQ(c.merge, 4);
|
||||
CHECK_EQ(c.temporal, 1);
|
||||
CHECK_EQ(c.base_size, 1024);
|
||||
CHECK_EQ(c.image_size, 1024);
|
||||
CHECK_EQ(c.tile, 640);
|
||||
CHECK_EQ(c.min_tiles, 2);
|
||||
CHECK_EQ(c.max_tiles, 32);
|
||||
CHECK(c.crop_mode);
|
||||
CHECK_EQ(c.factor(), 64);
|
||||
CHECK_EQ(c.patch_dim(), (int64_t)3 * 16 * 16);
|
||||
CHECK_EQ(c.mean[0], 0.5f);
|
||||
CHECK_EQ(c.stdv[2], 0.5f);
|
||||
CHECK(c.image_token == "<image>");
|
||||
|
||||
/* Only model_type: every processor key takes the reference's default. */
|
||||
const char* one_key[] = { "model_type" };
|
||||
const char* one_val[] = { "unlimited_ocr" };
|
||||
RadModelMeta mt{};
|
||||
mt.n_kv = 1;
|
||||
mt.kv_key = one_key;
|
||||
mt.kv_val = one_val;
|
||||
VisionConfig d;
|
||||
REQUIRE_EQ(d.from_meta(mt, &why), RAD_OK);
|
||||
CHECK_EQ(d.patch, 16);
|
||||
CHECK_EQ(d.downsample, 4);
|
||||
CHECK_EQ(d.base_size, 1024);
|
||||
CHECK_EQ(d.image_size, 1024);
|
||||
CHECK_EQ(d.tile, 640);
|
||||
CHECK_EQ(d.min_tiles, 2);
|
||||
CHECK_EQ(d.max_tiles, 32);
|
||||
CHECK(d.crop_mode);
|
||||
CHECK(d.image_token == "<image>");
|
||||
|
||||
/* PIL's bicubic is the only filter this processor stages. */
|
||||
const char* bad_key[] = { "model_type", "preprocessor.resample" };
|
||||
const char* bad_val[] = { "unlimited_ocr", "2" };
|
||||
RadModelMeta mb{};
|
||||
mb.n_kv = 2;
|
||||
mb.kv_key = bad_key;
|
||||
mb.kv_val = bad_val;
|
||||
VisionConfig b;
|
||||
CHECK_EQ(b.from_meta(mb, &why), RAD_E_UNSUPPORTED);
|
||||
}
|
||||
|
||||
/* The transposed ratios whose tall side the reference's Python set visits last, which is what its
|
||||
* tie rule needs beyond "the wider grid wins": the table in core/mm/processor.cpp must be the
|
||||
* golden's, pair for pair. */
|
||||
TEST(unlimitedocr_tie_order_is_the_references) {
|
||||
int32_t listed = 0;
|
||||
for (int32_t c = 1; c <= kUocrMaxTiles; ++c)
|
||||
for (int32_t r = c + 1; r <= kUocrMaxTiles; ++r) {
|
||||
if ((int64_t)c * r < kUocrMinTiles || (int64_t)c * r > kUocrMaxTiles) continue;
|
||||
bool want = false;
|
||||
for (int32_t k = 0; k < kUocrNTallLast; ++k)
|
||||
if (kUocrTallLast[k].c == c && kUocrTallLast[k].r == r) want = true;
|
||||
if (want) ++listed;
|
||||
if (uocr_tall_last(c, r) != want)
|
||||
std::fprintf(stderr, " pair %dx%d: %s last here, %s in the reference\n", c, r,
|
||||
uocr_tall_last(c, r) ? "tall" : "wide",
|
||||
want ? "tall" : "wide");
|
||||
CHECK_EQ((int)uocr_tall_last(c, r), (int)want);
|
||||
}
|
||||
CHECK_EQ(listed, kUocrNTallLast);
|
||||
}
|
||||
|
||||
/* dynamic_preprocess's grid for a sweep of shapes, the exact ties of its ratio set included. */
|
||||
TEST(unlimitedocr_tile_grids_are_the_references) {
|
||||
for (const GoldUocrGrid& g : kUocrGrid) {
|
||||
int32_t cols = 1, rows = 1;
|
||||
/* The reference asks dynamic_preprocess only for a picture larger than one tile. */
|
||||
if (g.w > kUocrTile || g.h > kUocrTile)
|
||||
uocr_grid(g.h, g.w, kUocrMinTiles, kUocrMaxTiles, kUocrTile, &cols, &rows);
|
||||
if (cols != g.cols || rows != g.rows)
|
||||
std::fprintf(stderr, " picture %lldx%lld: grid %dx%d, reference %dx%d\n",
|
||||
(long long)g.w, (long long)g.h, cols, rows, g.cols, g.rows);
|
||||
CHECK_EQ(cols, g.cols);
|
||||
CHECK_EQ(rows, g.rows);
|
||||
}
|
||||
}
|
||||
|
||||
/* Whole pictures in both modes: the segments (a line of tiles per tile row, the global view, the
|
||||
* view separator), every placeholder the picture marker, the pixel count the reference's layout
|
||||
* has, and the patches of the global view and of the tile mosaic. */
|
||||
TEST(unlimitedocr_pictures_are_the_references) {
|
||||
Processor p;
|
||||
REQUIRE_EQ(p.init(uocr_cfg(), [](const std::string&) { return std::vector<int32_t>{}; },
|
||||
uocr_marker), RAD_OK);
|
||||
const int32_t q = kUocrTile / kUocrPatch;
|
||||
for (const GoldUocrPic& g : kUocrPictures) {
|
||||
ImageMode mode;
|
||||
if (g.mode == 1) {
|
||||
mode.kind = ImageMode::Gundam;
|
||||
} else {
|
||||
mode.kind = ImageMode::Base;
|
||||
mode.image_size = g.mode == 2 ? kUocrBaseSize : kUocrTile;
|
||||
}
|
||||
std::shared_ptr<Item> it;
|
||||
std::string why;
|
||||
const int rc = p.image_from(frame(g.f, g.h, g.w), &it, &why, mode);
|
||||
if (rc < 0) std::fprintf(stderr, " %dx%d mode %d: %s\n", g.w, g.h, g.mode, why.c_str());
|
||||
CHECK_OK(rc);
|
||||
REQUIRE(it != nullptr);
|
||||
REQUIRE_EQ(it->n_seg, g.n_seg);
|
||||
REQUIRE_EQ((int64_t)it->expand.size(), (int64_t)g.n_ids);
|
||||
CHECK_EQ(it->n_rows(), (int64_t)g.n_ids);
|
||||
REQUIRE_EQ(it->seg_at.size(), (size_t)it->n_seg);
|
||||
for (int32_t v : it->expand) CHECK_EQ(v, kUocrImageToken);
|
||||
CHECK(it->rope_1d);
|
||||
CHECK(it->patch_raster);
|
||||
|
||||
int32_t cols = 0, rows = 0;
|
||||
for (int32_t s = 0; s < it->n_seg; ++s) {
|
||||
const GoldUocrSeg& gs = kUocrSegs[g.seg0 + s];
|
||||
CHECK_EQ(it->seg_h(s), gs.h);
|
||||
CHECK_EQ(it->seg_w(s), gs.w);
|
||||
CHECK_EQ(it->seg_at[(size_t)s], (int32_t)it->row_at(s));
|
||||
const int64_t np = it->patch_at(s + 1) - it->patch_at(s);
|
||||
CHECK_EQ(np, (int64_t)gs.h * gs.w);
|
||||
if (gs.h == q && s + 2 < it->n_seg) { /* a line of tiles */
|
||||
if (cols) { CHECK_EQ(cols, gs.real / q); }
|
||||
cols = gs.real / q;
|
||||
rows += 1;
|
||||
}
|
||||
/* Raster order: patch k of a segment sits at (k / w, k % w). */
|
||||
std::vector<int32_t> planes((size_t)(4 * np));
|
||||
it->coords(it->patch_at(s), np, planes.data());
|
||||
for (int64_t k = 0; k < np; ++k) {
|
||||
CHECK_EQ(planes[(size_t)k], (int32_t)(k / gs.w));
|
||||
CHECK_EQ(planes[(size_t)(np + k)], (int32_t)(k % gs.w));
|
||||
CHECK_EQ(planes[(size_t)(2 * np + k)], gs.h);
|
||||
CHECK_EQ(planes[(size_t)(3 * np + k)], gs.w);
|
||||
}
|
||||
}
|
||||
const bool tiled_ref = g.mode == 1 && g.cols * g.rows > 1;
|
||||
if (cols != (tiled_ref ? g.cols : 0) || rows != (tiled_ref ? g.rows : 0))
|
||||
std::fprintf(stderr, " picture %dx%d mode %d: %d lines of %d tiles, reference %dx%d\n",
|
||||
g.w, g.h, g.mode, rows, cols, g.cols, g.rows);
|
||||
CHECK_EQ(cols, tiled_ref ? g.cols : 0);
|
||||
CHECK_EQ(rows, tiled_ref ? g.rows : 0);
|
||||
|
||||
/* The global view, then the mosaic of the crops (if any). */
|
||||
const int32_t sg = it->n_seg - 2;
|
||||
const GoldUocrSeg& gg = kUocrSegs[g.seg0 + sg];
|
||||
if (fnv(segment_pixels(*it, sg, gg.real)) != g.global_fnv)
|
||||
std::fprintf(stderr, " picture %dx%d mode %d: the global view's patches differ\n",
|
||||
g.w, g.h, g.mode);
|
||||
CHECK_EQ(fnv(segment_pixels(*it, sg, gg.real)), g.global_fnv);
|
||||
std::vector<uint16_t> local;
|
||||
for (int32_t s = 0; s < sg; ++s) {
|
||||
const std::vector<uint16_t> v = segment_pixels(*it, s, kUocrSegs[g.seg0 + s].real);
|
||||
local.insert(local.end(), v.begin(), v.end());
|
||||
}
|
||||
if (g.local_fnv) {
|
||||
if (fnv(local) != g.local_fnv)
|
||||
std::fprintf(stderr, " picture %dx%d mode %d: the crops' patches differ\n",
|
||||
g.w, g.h, g.mode);
|
||||
CHECK_EQ(fnv(local), g.local_fnv);
|
||||
} else {
|
||||
CHECK(local.empty());
|
||||
}
|
||||
/* The token count is the reference's arithmetic. */
|
||||
CHECK_EQ(uocr_tokens(p.config(), mode, g.cols, g.rows), (int64_t)g.n_ids);
|
||||
}
|
||||
}
|
||||
|
||||
/* Pillow's antialiased bicubic runs its two passes in its own order (libImaging/Resample.c: the
|
||||
* vertical one first when the height shrinks by more than twice what the width does) and rounds
|
||||
* the intermediate to eight bits; this pins the order at sizes where the choice changes the
|
||||
* picture, since a one-pixel difference is invisible in a summary and visible to the model. */
|
||||
TEST(pillow_bicubic_resizes_in_the_references_pass_order) {
|
||||
for (const GoldUocrResize& g : kUocrResize) {
|
||||
Frame out;
|
||||
resize_bicubic(frame(0, g.sh, g.sw), g.dw, g.dh, &out);
|
||||
CHECK_EQ(out.w, g.dw);
|
||||
CHECK_EQ(out.h, g.dh);
|
||||
if (fnv(out.rgb) != g.fnv)
|
||||
std::fprintf(stderr, " %dx%d -> %dx%d: pixels differ\n", g.sw, g.sh, g.dw, g.dh);
|
||||
CHECK_EQ(fnv(out.rgb), g.fnv);
|
||||
}
|
||||
}
|
||||
|
||||
/* The runs: one per segment, every token the picture marker, and a 1-D rotary that gives each
|
||||
* placeholder its own position. The reference's assembled order is the item's row order -- the
|
||||
* local lines, the global view's lines, then the view separator. */
|
||||
TEST(unlimitedocr_runs_follow_the_reference_layout) {
|
||||
Processor p;
|
||||
REQUIRE_EQ(p.init(uocr_cfg(), [](const std::string&) { return std::vector<int32_t>{}; },
|
||||
uocr_marker), RAD_OK);
|
||||
ImageMode gundam;
|
||||
gundam.kind = ImageMode::Gundam;
|
||||
std::shared_ptr<Item> it;
|
||||
std::string why;
|
||||
CHECK_OK(p.image_from(frame(1, 900, 1400), &it, &why, gundam));
|
||||
REQUIRE(it != nullptr);
|
||||
|
||||
auto pm = std::make_shared<PromptMedia>();
|
||||
pm->items.push_back({ it, 1 }); /* after one text token */
|
||||
pm->layout();
|
||||
REQUIRE_EQ(pm->rope.runs.size(), (size_t)it->n_seg);
|
||||
for (int32_t s = 0; s < it->n_seg; ++s) {
|
||||
const RopeLayout::Run& r = pm->rope.runs[(size_t)s];
|
||||
CHECK(r.flat);
|
||||
CHECK_EQ(r.tok, 1 + it->seg_at[(size_t)s]);
|
||||
CHECK_EQ(r.n, (int32_t)(it->row_at(s + 1) - it->row_at(s)));
|
||||
CHECK_EQ(r.base, 1 + it->seg_at[(size_t)s]);
|
||||
CHECK_EQ(r.shift, 0);
|
||||
CHECK_EQ(r.item, 0);
|
||||
CHECK_EQ(r.row0, (int32_t)it->row_at(s));
|
||||
}
|
||||
const int64_t n = (int64_t)it->expand.size();
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
int32_t pos[3];
|
||||
pm->rope.at(1 + i, pos);
|
||||
CHECK_EQ(pos[0], (int32_t)(1 + i));
|
||||
CHECK_EQ(pos[1], (int32_t)(1 + i));
|
||||
CHECK_EQ(pos[2], (int32_t)(1 + i));
|
||||
}
|
||||
/* The reference's arithmetic, spelled out: the global view's 16 lines of 16 features with a
|
||||
* newline each plus the separator, then the local grid's lines (a newline after each). */
|
||||
const VisionConfig& c = p.config();
|
||||
CHECK_EQ(uocr_tokens(c, gundam, 1, 1), (int64_t)273);
|
||||
ImageMode base;
|
||||
base.kind = ImageMode::Base;
|
||||
CHECK_EQ(uocr_tokens(c, base, 1, 1), (int64_t)273);
|
||||
ImageMode page640;
|
||||
page640.kind = ImageMode::Base;
|
||||
page640.image_size = kUocrTile;
|
||||
CHECK_EQ(uocr_tokens(c, page640, 1, 1), (int64_t)111);
|
||||
CHECK_EQ(uocr_tokens(c, gundam, 3, 2), (int64_t)273 + (int64_t)(10 * 3 + 1) * 20);
|
||||
CHECK_EQ(uocr_tokens(c, gundam, 4, 3), (int64_t)273 + (int64_t)(10 * 4 + 1) * 30);
|
||||
|
||||
/* A request may restate the global view's side, but not cut crops at another tile or pad a
|
||||
* page to a side that is not whole merged patches. */
|
||||
bool crop = false;
|
||||
int32_t bs = 0, is = 0;
|
||||
ImageMode b2048;
|
||||
b2048.kind = ImageMode::Gundam;
|
||||
b2048.base_size = 2048;
|
||||
CHECK_EQ(uocr_mode(c, b2048, &crop, &bs, &is, &why), RAD_OK);
|
||||
CHECK_EQ(uocr_tokens(c, b2048, 1, 1), (int64_t)(32 + 1) * 32 + 1);
|
||||
ImageMode tile512;
|
||||
tile512.kind = ImageMode::Gundam;
|
||||
tile512.image_size = 512;
|
||||
CHECK_EQ(uocr_mode(c, tile512, &crop, &bs, &is, &why), RAD_E_INVAL);
|
||||
ImageMode odd;
|
||||
odd.kind = ImageMode::Base;
|
||||
odd.image_size = 700;
|
||||
CHECK_EQ(uocr_mode(c, odd, &crop, &bs, &is, &why), RAD_E_INVAL);
|
||||
ImageMode ok640;
|
||||
ok640.kind = ImageMode::Base;
|
||||
ok640.image_size = 640;
|
||||
REQUIRE_EQ(uocr_mode(c, ok640, &crop, &bs, &is, &why), RAD_OK);
|
||||
CHECK(!crop);
|
||||
CHECK_EQ(is, 640);
|
||||
|
||||
/* The multi-page form: two pages at the base mode are two runs of 273, back to back. */
|
||||
ImageMode base2;
|
||||
base2.kind = ImageMode::Base;
|
||||
std::shared_ptr<Item> a, b;
|
||||
CHECK_OK(p.image_from(frame(0, 300, 500), &a, &why, base2));
|
||||
CHECK_OK(p.image_from(frame(1, 900, 1400), &b, &why, base2));
|
||||
auto mp = std::make_shared<PromptMedia>();
|
||||
mp->items.push_back({ a, 1 });
|
||||
mp->items.push_back({ b, 1 + (int32_t)a->expand.size() });
|
||||
mp->layout();
|
||||
CHECK_EQ((int64_t)mp->rope.runs.size(), (int64_t)4);
|
||||
CHECK_EQ(a->expand.size(), (size_t)273);
|
||||
CHECK_EQ(b->expand.size(), (size_t)273);
|
||||
int32_t pos[3];
|
||||
mp->rope.at(1 + 273 + 100, pos);
|
||||
CHECK_EQ(pos[0], (int32_t)(1 + 273 + 100));
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
Reference in new issue
Block a user