keyframes: feed endpoint keyframes through the vision tower (TE-anchor tokens)

Image-conditioned MiniMax-H3 (I2VA/L2VA/FL2VA = first_frame/last_frame) rendered
over-saturated / posterized while T2VA was clean on the same container. Root cause:
the reference conditions a keyframe TWICE -- a vision-tower anchor (clip.tokenize
images=[kf] -> <Picture N> soft-tokens in the text conditioning) AND a VAE-latent pin --
but Radiance did only the VAE pin for endpoint keyframes. The absent semantic anchor
left the DiT reconstructing the frame from low-level VAE pixels alone -> posterize /
black-outline / oversaturation.

Route endpoint keyframes through the existing vision-tower media path, so they produce
<Picture N> anchor soft-tokens in addition to their VAE pin (the reference's dual
channel). No new kernels, no ABI / arena change: build_cond stages r->media generically
and the arch scatter is the generic op_mm.

  - videos.cpp: keyframe index = <Picture> ordinal (first=1,last=2); stable-sort keys
    by kind before merge; run the prepare+placement loops for FIRST/LAST_FRAME as well
    as IMAGE (area-matched tower form, aspect-kept; the exact-canvas VAE pin is a
    separate, unchanged second encode); budget wording covers keyframes.
  - video_driver.cpp: a towerless container no longer refuses a keyframe request (skip
    FIRST/LAST_FRAME in the "needs a vision tower" scan) -> pin-only path runs; the
    encodes guard still requires the VAE encoder and now labels a keyframe correctly.

Device A/B (uncensored w8a8, TP2, seed 1234, 512^2): prompt tokens i2va 27->291,
fl2va 1560->2088 (the +256 / +2*256 keyframe vision tokens + labels), t2va 27->27.
T2VA output is bit-identical across binaries (text path untouched). Image-conditioned
output diverges (cos 0.77/0.80): the realistic I2VA mid-frame recovers foliage
structure (blobs -> trees), softens outlines and shadow detail toward the clean control;
FL2VA gains semantic adherence (prompted "wings spread" now renders). Endpoint-pin
cosine unchanged (0.971 / 0.988 / 0.980, at/above the 0.985 bar). No budget reject.

Tests: fl2va keyframes now feed the tower and still pin the ends (index 1/2, token
stream with both vision blocks); towerless fl2va pins-only; driver-level keyframe
conditions through the tower and needs no tower. NOTE-REF2VA.md #8 closed.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
gearwave00001andClaude committed 2026-10-21 01:06:26 -05:00
1 parent 8785774fc0
commit cd393a0973
6 files changed
+212 -26

No files matched your search

+9 -1
View File
@@ -85,7 +85,15 @@ struct VideoRefMedia {
* each into the pack's keyframe table and encodes it into an image slot (a keyframe request
* carries no references, so those slots are free). The server resizes each to the target canvas
* (first a plain stretch, last a centre crop) and sets enc_pixels to one frame at that size, so
* its latent grid is the target's exactly and its pinned rows are frame_rows wide. */
* its latent grid is the target's exactly and its pinned rows are frame_rows wide.
* A keyframe is fed to the DiT on TWO channels, matching the reference (clip.tokenize +
* vae.encode): the VAE-latent pin above, AND -- when the container carries a vision tower -- a
* <Picture k> anchor block through the tower (its `data` bytes are prepared as media and its
* soft tokens interleaved into the text conditioning). That anchor is the semantic counterpart
* to the low-level VAE pin; without it the DiT reconstructs the frame from pixels alone and
* over-saturates. `index` is the keyframe's <Picture> ordinal (first = 1, last = 2), not a
* per-kind count, so the block is labelled the way the source workflow names it. On a towerless
* container the anchor channel is absent and the pin-only path runs unchanged. */
enum Kind : int32_t { IMAGE = 0, VIDEO = 1, VIDEO_AUDIO = 2, AUDIO = 3, FIRST_FRAME = 4, LAST_FRAME = 5 };
Kind kind = IMAGE;
int32_t index = 0;
+17 -5
View File
@@ -345,7 +345,15 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
* pack lays those slots between the text and the target streams (RadVideoGenBatch::ref_tab).
* Every kind the declaration allows is consumed; the guard below is only about a container
* lacking its vision tower, which could encode nothing. */
if (!r->refs.empty() && max_enc_ == 0)
/* Reference media need the vision tower (their identity soft tokens). Endpoint keyframes do not:
* they condition through the VAE-latent pin, so a container converted without its tower can still
* render them -- the server simply lays no `media` for them and the pin-only path runs. (Keyframes
* still need the VAE encoders, checked just below.) */
bool has_ref_media = false;
for (const VideoRefMedia& m : r->refs)
if (m.kind != VideoRefMedia::FIRST_FRAME && m.kind != VideoRefMedia::LAST_FRAME)
has_ref_media = true;
if (has_ref_media && max_enc_ == 0)
return no("this container was converted without its vision tower, so no request can "
"carry reference media");
/* A reference video, soundtrack or standalone audio reaches the model only through the VAE
@@ -358,12 +366,16 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
if (m.kind != VideoRefMedia::IMAGE)
return no("'" + m.name + "' is a reference " +
(m.kind == VideoRefMedia::VIDEO ? "video"
: m.kind == VideoRefMedia::VIDEO_AUDIO ? "soundtrack" : "audio") +
: m.kind == VideoRefMedia::VIDEO_AUDIO ? "soundtrack"
: m.kind == VideoRefMedia::FIRST_FRAME ||
m.kind == VideoRefMedia::LAST_FRAME ? "keyframe" : "audio") +
", but this container was converted without its VAE encoders, so nothing "
"can condition on it");
/* Only reference images add language-model soft-token rows, so only they need processed media.
* A reference video or audio reaches the model purely through the VAE encode rows the pack lays
* from the reference table, so a request of those alone carries no `media` and needs none. */
/* Only pictures (reference images and endpoint keyframes) add language-model soft-token rows, so
* only they need processed media. A reference video or audio reaches the model purely through the
* VAE encode rows the pack lays from the reference table, so a request of those alone carries no
* `media` and needs none. A keyframe with no tower also carries no `media` — that is the deliberate
* pin-only fallback, not a silent weakening (its VAE pin still conditions the DiT). */
bool has_image = false;
for (const VideoRefMedia& m : r->refs)
if (m.kind == VideoRefMedia::IMAGE) has_image = true;
+28 -8
View File
@@ -310,7 +310,11 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
gen::VideoRefMedia m;
m.kind = p.name == "first_frame" ? gen::VideoRefMedia::FIRST_FRAME
: gen::VideoRefMedia::LAST_FRAME;
m.index = 1;
/* The keyframe's <Picture k> ordinal, so its vision-anchor block is labelled the way
* the source workflow names it (first = <Picture 1>, last = <Picture 2>). A keyframe
* request carries no reference media (checked below), so these numbers cannot collide
* with a reference's own <Picture k>. */
m.index = m.kind == gen::VideoRefMedia::FIRST_FRAME ? 1 : 2;
m.name = p.name;
m.filename = p.filename;
m.content_type = p.content_type;
@@ -603,9 +607,15 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
r->refs = std::move(refs);
/* Keyframes ride the reference vector for the driver's sake (its encode queue indexes it), but
* always AFTER every reference: the driver lays the reference rows down first, then folds each
* FIRST_FRAME/LAST_FRAME into the pack's keyframe table. first before last, so they keyframe-order
* 0 and 1 and take image slots 1 and 2. */
* FIRST_FRAME/LAST_FRAME into the pack's keyframe table. Sort first before last (a client may send
* the file parts in either order) so they keyframe-order 0 and 1, take image slots 1 and 2, and
* present as <Picture 1> then <Picture 2>. */
std::stable_sort(keys.begin(), keys.end(), [](const gen::VideoRefMedia& a,
const gen::VideoRefMedia& b) {
return a.kind < b.kind; /* FIRST_FRAME (4) before LAST_FRAME (5) */
});
r->refs.insert(r->refs.end(), keys.begin(), keys.end());
const bool has_keys = !keys.empty();
/* ---- the prompt: the template's text with special tokens, the reference media in its
* presentation order, the prompt last (docs/PORT-REF2VA.md §3: images, then videos each
@@ -630,7 +640,7 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
* soundtrack's `<Audio j>` label is emitted right before its video's `<Video k>` label, and
* `audio_ord` counts soundtracks and standalone audios together, in the order they are
* spoken (docs/PORT-REF2VA.md §3). */
if (ref2va && deps_.mm && deps_.mm->accepts(MediaKind::Image)) {
if ((ref2va || has_keys) && deps_.mm && deps_.mm->accepts(MediaKind::Image)) {
/* Every picture is decoded and processed before any token is laid down. A reference
* video or audio needs no vision item here — it reaches the model through the VAEs, so
* it contributes only its presentation label to the prompt (the 2 fps timestamped
@@ -638,8 +648,18 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
std::vector<std::shared_ptr<mm::Item>> pics;
MediaOpts mo;
mo.max_pixels = (int64_t)W * (int64_t)H; /* `match`: scaled to the output's area */
/* A reference image and an endpoint keyframe are both fed to the vision tower: the keyframe
* gets a <Picture k> anchor block here (the soft tokens the reference also feeds) IN
* ADDITION to the VAE-latent pin it takes from `enc_pixels` below — the reference's own
* split (clip.tokenize + vae.encode). A reference picture and a keyframe never share a
* request, so `pics` holds one or the other. */
auto anchor = [](const gen::VideoRefMedia& m) {
return m.kind == gen::VideoRefMedia::IMAGE ||
m.kind == gen::VideoRefMedia::FIRST_FRAME ||
m.kind == gen::VideoRefMedia::LAST_FRAME;
};
for (const gen::VideoRefMedia& m : r->refs) {
if (m.kind != gen::VideoRefMedia::IMAGE) continue;
if (!anchor(m)) continue;
const std::vector<uint8_t> bytes(m.data.begin(), m.data.end());
std::shared_ptr<mm::Item> it;
std::string why;
@@ -662,7 +682,7 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
if (!pics.empty()) media = std::make_shared<mm::PromptMedia>();
size_t i = 0;
for (const gen::VideoRefMedia& m : r->refs) { /* sorted by (kind, index): images first */
if (m.kind != gen::VideoRefMedia::IMAGE) continue;
if (!anchor(m)) continue;
put("<Picture " + std::to_string(m.index) + ">: ", false);
mm::Placed pl;
pl.item = pics[i++];
@@ -691,8 +711,8 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
put(t.substr(p + kSlot.size()), true);
}
if ((int64_t)r->tokens.size() > info.max_text)
return bad("prompt", "the prompt" + std::string(ref2va ? " with its reference media"
: "") +
return bad("prompt", "the prompt" + std::string((ref2va || has_keys)
? " with its media" : "") +
" is " + std::to_string(r->tokens.size()) +
" tokens; this model takes at most " +
std::to_string(info.max_text));
+20 -5
View File
@@ -556,11 +556,26 @@ for debug work on every encode. **Device-verified at 512²:** FL2VA 512×512 com
survives, no band fault); host geom/video/ref2va/encoding tests green. Core ops are within their declared bands
(a 1-frame 512 level plane is `0.235×` the 17·256² they're sized for).
**Remaining fidelity gap (→ #10 / P6 bucket, not a function gap).** The pins-only path **skips `clip.tokenize`
vision tokens** for the keyframe images (the TE-anchor tokens the reference would also feed), so a prompt that
*contradicts* the pinned image can let the text win. Closing this = feed TE-anchor tokens for keyframe rows;
that is a value-fidelity follow-up, and the earlier "no golden engine to diff against" limit applies to it.
Chaining still also works independently via the ref_video slot0 path.
**Was a remaining fidelity gap; now CLOSED (2026-10-21, task #52).** The pins-only path used to **skip
`clip.tokenize` vision tokens** for the keyframe images (the TE-anchor tokens the reference would also feed),
so a prompt that *contradicts* the pinned image could let the text win, and realistic I2VA/FL2VA output
over-saturated/posterized (root cause of #37/#50, see memory `dasiwa-reference-ground-truth`). This is now
shipped: an endpoint keyframe is fed the DiT on BOTH channels the reference uses — the VAE-latent pin AND a
`<Picture k>` vision-tower anchor block interleaved into the text conditioning. Mechanism: `videos.cpp` now
runs the media/`<Picture N>` block for keyframe requests (first_frame→`<Picture 1>`, last_frame→`<Picture 2>`),
preparing each anchor through the SAME `mm::prepare`/`PromptMedia`/`mm_rows` tower path the reference images
use (a second, independent encode of the keyframe — the tower sees the aspect-kept area-matched image, the
pin sees the stretch/cover exact canvas), IN ADDITION to the `enc_pixels` pin at `videos.cpp:709-730`. The
vision tower is optional: a container converted WITHOUT it still renders keyframes pin-only (`video_driver.cpp`
tower guard now skips FIRST/LAST_FRAME; keyframes still require the VAE `encodes`). Host tests
`fl2va_keyframes_feed_the_tower_and_pin_the_output_ends`, `fl2va_without_a_tower_pins_only` (videos_test) and
`a_keyframe_completion_also_conditions_through_the_tower`, `a_keyframe_completion_needs_no_vision_tower`
(video_gen_test) cover both channels and both tower-present/absent paths. The earlier "no golden engine to
diff against" limit still bounds EXACT value-parity (→ #10/P6): the reference embeds `<Picture N>` inline in
its prompt prose while Radiance prepends the vision block before the prompt (the repo's PORT-REF2VA §3
convention, which the reference tokenizer also uses for the block itself); anchor PRESENCE is what this change
delivers. Chaining also still works independently via the ref_video slot0 path. Device qualitative check (does
the posterize drop) is the remaining validation.
**Endpoint parity re-confirmed at 512 (2026-10-19, post-fix).** Same method (frame-0/last-frame cosine vs the
resized anchor, 256² sampling space, RGB-mean-centred), on the fixed binary at 512²·12 s·25 step·seed 1234:
+75
View File
@@ -985,6 +985,81 @@ TEST(a_keyframe_completes_widen_the_step_and_pin_the_ends) {
CHECK(saw_step);
}
/* The keyframe's SECOND channel: when the container carries a vision tower, a keyframe request that
* also carries processed media runs a conditioning pass that stages the tower's rows (the anchor
* tokens) ALONGSIDE the VAE pin. So a request yields BOTH a VENCODE pass (the pin at image slot 1) and
* a VCOND pass (the tower run), exactly as a reference picture conditions -- the keyframe is not merely
* a pin once a tower is present. */
TEST(a_keyframe_completion_also_conditions_through_the_tower) {
VideoFixture f;
f.build();
with_tower(f, 16);
RadVideoGenDecl& d = f.prog.video_gen;
d.encodes = 1;
d.enc_ratio = 16;
d.max_enc_frames = 1;
d.max_enc_side = 16;
d.ref_rows = 1;
CHECK_OK(f.start_with_pack_encode(fake_pack_ref, fake_encode_geom));
std::string why;
auto r = f.req(2);
r->tokens = { 1, 700, 701, 2, 3 }; /* a <Picture 1> anchor run sits in the prompt */
gen::VideoRefMedia first = ref_part(gen::VideoRefMedia::FIRST_FRAME, "first_frame", 1);
first.enc_frames = 1;
first.enc_h = 16;
first.enc_w = 16;
first.enc_pixels.assign(16 * 16 * 3, 1.0f);
r->refs.push_back(first);
r->media = picture_media(6, 1); /* the SAME keyframe also feeds the tower */
CHECK_OK(f.drv.submit(std::move(r), &why));
bool saw_cond = false, saw_pin = false;
for (int i = 0; i < 20; ++i) {
const RadBatch* b = f.drv.next(i);
if (!b) break;
if (b->gen == RAD_GEN_PASS_VCOND) {
saw_cond = true;
CHECK_EQ(b->n_mm_rows, (int64_t)2); /* the keyframe's tower run */
CHECK(b->mm_rows != nullptr);
CHECK_EQ(b->enc_n_patch, (int64_t)8); /* its patches staged for the tower */
CHECK(b->enc_pixels != nullptr);
} else if (b->gen == RAD_GEN_PASS_VENCODE) {
saw_pin = true;
CHECK_EQ(b->vgen_b->enc_kind, (int32_t)RAD_ENC_IMAGE);
CHECK_EQ(b->vgen_b->enc_slot, (int32_t)1);
}
f.answer(10 * i);
CHECK_OK(f.drv.done());
}
CHECK(saw_cond); /* the anchor channel ran ... */
CHECK(saw_pin); /* ... alongside the VAE pin */
}
/* A keyframe is a VAE-latent pin, so it conditions through the encoders, NOT the vision tower: a
* container converted WITHOUT its tower still renders it (pin-only). Only reference media -- whose
* identity soft tokens come from the tower -- are refused on such a container. */
TEST(a_keyframe_completion_needs_no_vision_tower) {
VideoFixture f;
f.build(); /* no with_tower: the container carries no vision tower */
RadVideoGenDecl& d = f.prog.video_gen;
d.encodes = 1; /* it does carry the VAE encoders, which the pin needs */
d.enc_ratio = 16;
d.max_enc_frames = 1;
d.max_enc_side = 16;
d.ref_rows = 1;
CHECK_OK(f.start_with_pack_encode(fake_pack_ref, fake_encode_geom));
std::string why;
auto r = f.req(2);
r->tokens = { 1, 2, 3 }; /* no tower run: no media placed */
gen::VideoRefMedia first = ref_part(gen::VideoRefMedia::FIRST_FRAME, "first_frame", 1);
first.enc_frames = 1;
first.enc_h = 16;
first.enc_w = 16;
first.enc_pixels.assign(16 * 16 * 3, 1.0f);
r->refs.push_back(first);
CHECK_OK(f.drv.submit(std::move(r), &why)); /* the pin-only path admits without a tower */
}
/* What the container cannot consume is refused at submit, naming the part or the budget it breaks —
* never silently dropped into a weaker video. A reference video, soundtrack or audio needs the VAE
* encoders; on a container converted without them (encodes 0) they are refused by name here, and on
+63 -7
View File
@@ -556,11 +556,14 @@ TEST(ref2va_videos_and_audios_are_encoded_and_spoken) {
/* Endpoint keyframes (task fl2va): a first and a last frame reach the generator as FIRST_FRAME and
* LAST_FRAME references, each resized to the EXACT output canvas -- a keyframe is a PIN, so its
* latent grid must be the target's, not an aspect-kept snap like a reference's. The pins-only path
* does not show them to the language model, so no media is built. The driver's fold into the pack's
* keyframe rows and the device scatter are the engine/device tests; this is the server half. */
TEST(fl2va_keyframes_pin_the_output_ends) {
* latent grid must be the target's, not an aspect-kept snap like a reference's. A keyframe is fed the
* DiT on TWO channels, matching the reference (clip.tokenize + vae.encode): the VAE pin AND a
* <Picture k> vision-tower anchor block interleaved into the prompt (when the container carries a
* tower). The driver's fold into the pack's keyframe rows and the device scatter are the engine/device
* tests; this is the server half. */
TEST(fl2va_keyframes_feed_the_tower_and_pin_the_output_ends) {
FakeMultimodal mm;
mm.expand = { 900, 901 };
mm.enc_frames = 5; mm.enc_side = 16; mm.enc_n = 850;
FakeVideoGen gen;
std::unique_ptr<Fixture> f(new Fixture(Fixture::Tweak{ [&](ServerOptions& o, Deps& d) {
@@ -581,20 +584,73 @@ TEST(fl2va_keyframes_pin_the_output_ends) {
FormPart{ "last_frame", "l.png", "image/png", "PL" } };
HttpResponse r = f->srv->router().dispatch(q);
REQUIRE_EQ(r.status, 200);
CHECK_EQ(mm.n_cenc, 2); /* both anchors canvas-resized ... */
CHECK_EQ(mm.n_ienc, 0); /* ... none snapped like a reference */
/* Both anchors reach the vision tower as images (the anchor channel) ... */
REQUIRE_EQ(mm.got.size(), (size_t)2);
CHECK(mm.got[0].first == MediaKind::Image);
CHECK(mm.got[0].second == std::vector<uint8_t>({ 'P', 'F' }));
CHECK(mm.got[1].second == std::vector<uint8_t>({ 'P', 'L' }));
/* ... and are BOTH canvas-resized for the VAE pin (the second, independent channel). */
CHECK_EQ(mm.n_cenc, 2);
CHECK_EQ(mm.n_ienc, 0); /* a keyframe is never snapped like a reference */
CHECK_EQ(mm.cenc_w, 256);
CHECK_EQ(mm.cenc_h, 256);
CHECK(mm.cenc_cover); /* the last call was the last frame's centre crop */
const gen::VideoRequest& g = gen.last();
REQUIRE_EQ(g.refs.size(), (size_t)2);
CHECK_EQ((int)g.refs[0].kind, (int)gen::VideoRefMedia::FIRST_FRAME);
CHECK_EQ(g.refs[0].index, 1); /* <Picture 1> = first */
CHECK_EQ((int)g.refs[1].kind, (int)gen::VideoRefMedia::LAST_FRAME);
CHECK_EQ(g.refs[1].index, 2); /* <Picture 2> = last */
CHECK_EQ(g.refs[0].enc_frames, 1);
CHECK_EQ(g.refs[0].enc_h, 256); /* the target canvas, exactly */
CHECK_EQ(g.refs[0].enc_w, 256);
CHECK_EQ(g.refs[0].enc_pixels.size(), (size_t)256 * 256 * 3);
CHECK(g.media == nullptr); /* pins-only: the anchors never reach the text encoder */
/* The anchors also reach the text encoder: a <Picture 1>/<Picture 2> block before the prompt. */
REQUIRE(g.media != nullptr);
REQUIRE_EQ(g.media->items.size(), (size_t)2);
CHECK(g.media->items[0].tok < g.media->items[1].tok);
std::vector<int32_t> want;
auto chars = [&](const std::string& s) { for (unsigned char c : s) want.push_back((int32_t)c); };
chars("<v>");
chars("<Picture 1>: ");
want.insert(want.end(), mm.expand.begin(), mm.expand.end());
chars("<Picture 2>: ");
want.insert(want.end(), mm.expand.begin(), mm.expand.end());
chars("fox");
CHECK(g.tokens == want);
}
/* Without a vision tower the keyframe request still renders: the anchors keep their VAE pin but carry
* no text-encoder media (the deliberate pin-only fallback, not a silent weakening). */
TEST(fl2va_without_a_tower_pins_only) {
FakeMultimodal mm;
mm.accept_image = false; /* a container converted without its vision tower */
mm.enc_frames = 5; mm.enc_side = 16; mm.enc_n = 850;
FakeVideoGen gen;
std::unique_ptr<Fixture> f(new Fixture(Fixture::Tweak{ [&](ServerOptions& o, Deps& d) {
d.videos = &gen;
d.mm = &mm;
o.video_model = true;
gen.inf.encodes = 1;
gen.inf.enc_ratio = 16;
gen.inf.max_enc_side = 512;
} }));
HttpRequest q;
q.method = "POST";
q.path = "/v1/videos";
q.form = { FormPart{ "prompt", "", "", "fox" },
FormPart{ "size", "", "", "256x256" },
FormPart{ "task", "", "", "fl2va" },
FormPart{ "first_frame", "f.png", "image/png", "PF" },
FormPart{ "last_frame", "l.png", "image/png", "PL" } };
HttpResponse r = f->srv->router().dispatch(q);
REQUIRE_EQ(r.status, 200);
CHECK_EQ(mm.got.size(), (size_t)0); /* nothing reached the absent tower */
CHECK_EQ(mm.n_cenc, 2); /* both anchors still canvas-resized for the pin */
const gen::VideoRequest& g = gen.last();
CHECK(g.media == nullptr); /* no text-encoder media on the pin-only path */
CHECK_EQ(g.refs[0].enc_pixels.size(), (size_t)256 * 256 * 3);
CHECK_EQ(g.refs[1].enc_pixels.size(), (size_t)256 * 256 * 3);
}
/* A named keyframe task must carry its frame: fl2va wants both ends. */