minimaxh3+r4d+driver: the prompt's vision pads are modulated as video, not as text

The reference modulates by a per-token TAG: token_tags_from_embeds_info tags
every image embed span (with one flanking token on each side) 0 -- video --
inside the prompt, while audio pads keep tag 1 (parallel PR #236, point 3).
Our port modulated the whole prompt span as text, so every reference photo's
pad tokens read the text ada projection where the reference reads the video
one. The tags arrive in the prompt itself and nothing else carries them, so
the denoiser could not know, and the look drifted with the reference count.

The driver now writes each image item's token span into a tail of ref_tab --
one count, then {start, length} per run, audio items excluded (the reference
tags images only). The tail rides inert through the pack and ref_parts, and
is present exactly when the reference table is, so an i2va request keeps its
old bytes. ref2va_mod_groups grows a runs argument: it cuts the prompt at the
runs' edges and gives the pad pieces the VIDEO cell -- the video projection
on the video-schedule block; the prompt's own timestep row never changes,
only the projection.

Cells stay few however many photos interleave, but groups no longer equal
cells: two prompt pieces separated by a pad share one cell non-adjacently.
So the merged-group output gains a per-group column map, and the modulated-
norm kernels -- libr4d and libref alike -- accept an optional trailing
colmap (slot 9 quant, 7 bf16, past every other arch's last operand) that
redirects sequence -> table column, relaxing the row-count checks it binds.
rad_arch_video_mod_segs exports boundaries then map, the plugin stages both
only for a reference request -- the plain path still stages boundaries alone,
byte-identical -- and the half-split keeps its single video row by taking
the target's column instead of the last one. max_ref_segs now bounds the
DISTINCT CELLS, its old meaning was the group count. ABI 28; no struct's
shape moved, the layout table is re-stamped.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
gearwave00001andClaude committed 2026-10-25 07:29:27 -05:00
1 parent c5483e3241
commit 8a6a1180ce
14 files changed
+548 -139

No files matched your search

+64 -15
View File
@@ -287,24 +287,73 @@ static void cell_of_kind(int kind, int64_t S, int* md, int64_t* r0) {
}
}
void ref2va_mod_groups(const Ref2VAPack& p, int64_t S, Ref2VAModGroups* out) {
out->bound.clear(); out->md.clear(); out->r0.clear();
/* Walk the segments in sequence order, opening a new group whenever the cell changes or a gap
* would otherwise appear. Empty segments carry no rows: they neither extend nor split a group,
* so a run of photos with an empty cond between them still rides one cell. */
int cur_md = 0; int64_t cur_r0 = 0; bool open = false;
int64_t prev_end = 0;
void ref2va_mod_groups(const Ref2VAPack& p, int64_t S, const std::vector<Ref2VATagRun>& runs,
Ref2VAModGroups* out) {
out->bound.clear(); out->col.clear(); out->md.clear(); out->r0.clear();
/* Normalise the tag runs: keep the part of each that falls inside the text segment, drop the
* rest, order by start, and trim a run that would overlap its predecessor -- runs describe
* disjoint token spans, but the driver's view and this one must never disagree by accident. */
std::vector<Ref2VATagRun> norm;
for (const Ref2VATagRun& r : runs) {
Ref2VATagRun c = r;
if (c.end <= c.start) continue;
norm.push_back(c);
}
std::sort(norm.begin(), norm.end(),
[](const Ref2VATagRun& a, const Ref2VATagRun& b) { return a.start < b.start; });
norm.erase(std::unique(norm.begin(), norm.end(),
[](const Ref2VATagRun& a, const Ref2VATagRun& b) {
return a.start == b.start && a.end == b.end && a.tag == b.tag;
}),
norm.end());
/* Lay the row pieces: each non-empty segment contributes its cell, except the text segment,
* which is cut at the (clamped) run edges into prompt pieces (text tag on the text block) and
* pad pieces (the run's tag, still on the video-schedule block -- the reference keeps the pad
* rows on the prompt's timestep row and changes only the ada projection). */
struct Piece { int64_t start, end; int md; int64_t r0; };
std::vector<Piece> pieces;
for (const Ref2VASeg& s : p.segs) {
if (s.end <= s.start) continue;
int md; int64_t r0;
cell_of_kind(s.kind, S, &md, &r0);
if (!open || md != cur_md || r0 != cur_r0 || s.start != prev_end) {
if (open) out->bound.push_back(s.start); /* close the previous group at this start */
else out->bound.push_back(0);
cur_md = md; cur_r0 = r0; open = true;
out->md.push_back(md); out->r0.push_back(r0);
if (s.kind == kR2VText) {
int64_t at = s.start;
for (const Ref2VATagRun& r : norm) {
const int64_t a = std::max(r.start, s.start), b = std::min(r.end, s.end);
if (b <= at) continue; /* fully covered already */
if (a >= s.end) break; /* the rest lies past the prompt */
if (a > at) pieces.push_back({ at, a, 1, 0 });
if (b > at) pieces.push_back({ std::max(a, at), b, r.tag, 0 });
at = std::max(at, b);
}
if (at < s.end) pieces.push_back({ at, s.end, 1, 0 });
continue;
}
prev_end = s.end;
Piece pc; pc.start = s.start; pc.end = s.end;
cell_of_kind(s.kind, S, &pc.md, &pc.r0);
pieces.push_back(pc);
}
/* Merge adjacent pieces that share a cell -- every merge the old segment walk made still
* happens (a photo run is one repeated cell), and the new pad pieces merge back into a
* prompt/neighbour piece exactly when their cells agree. Cells deduplicate into table
* COLUMNS: `md`/`r0` describe the distinct cells, `col` maps each group to its cell. */
int cur_col = -1; bool open = false;
int64_t prev_end = 0;
auto column_of = [&](int md, int64_t r0) {
for (size_t c = 0; c < out->md.size(); ++c)
if (out->md[c] == md && out->r0[c] == r0) return (int)c;
out->md.push_back(md); out->r0.push_back(r0);
return (int)out->md.size() - 1;
};
for (const Piece& pc : pieces) {
const int col = column_of(pc.md, pc.r0);
if (!open || col != cur_col || pc.start != prev_end) {
if (open) out->bound.push_back(pc.start); /* close the previous group at this start */
else out->bound.push_back(0);
cur_col = col; open = true;
out->col.push_back(col);
}
prev_end = pc.end;
}
if (open) out->bound.push_back(p.rows); /* the last group runs to the end of the sequence */
}