From cd393a0973dc009e3adcb0aa37f068b4ea6b061f Mon Sep 17 00:00:00 2001 From: gearwave00001 Date: Wed, 21 Oct 2026 01:06:26 -0500 Subject: [PATCH] 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] -> 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 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 = 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 --- core/gen/video.h | 10 +++++- core/gen/video_driver.cpp | 22 +++++++++--- core/server/videos.cpp | 36 ++++++++++++++----- docs/NOTE-REF2VA.md | 25 ++++++++++--- tests/video_gen_test.cpp | 75 +++++++++++++++++++++++++++++++++++++++ tests/videos_test.cpp | 70 ++++++++++++++++++++++++++++++++---- 6 files changed, 212 insertions(+), 26 deletions(-) diff --git a/core/gen/video.h b/core/gen/video.h index 8c26c584..01f03eb7 100644 --- a/core/gen/video.h +++ b/core/gen/video.h @@ -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 + * 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 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; diff --git a/core/gen/video_driver.cpp b/core/gen/video_driver.cpp index 62cc8673..b96b244a 100644 --- a/core/gen/video_driver.cpp +++ b/core/gen/video_driver.cpp @@ -345,7 +345,15 @@ int VideoDriver::submit(std::unique_ptr 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 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; diff --git a/core/server/videos.cpp b/core/server/videos.cpp index 24f9ab8c..1c76a26b 100644 --- a/core/server/videos.cpp +++ b/core/server/videos.cpp @@ -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 ordinal, so its vision-anchor block is labelled the way + * the source workflow names it (first = , last = ). A keyframe + * request carries no reference media (checked below), so these numbers cannot collide + * with a reference's own . */ + 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 then . */ + 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 `