From c5483e32411e030bab73d47d84bc5752a227410f Mon Sep 17 00:00:00 2001 From: gearwave00001 Date: Sun, 25 Oct 2026 06:58:58 -0500 Subject: [PATCH] videos: the tower reads a keyframe's own canvas frame, never a re-decode The reference feeds the vision tower the same resized tensor it hands the video VAE: an anchor's identity and its pinned latent are the SAME pixels. Our port showed the tower the source file at the `match` area instead, so a keyframe whose native aspect differed from the canvas reached the two channels differently (parallel PR #236, point 5). A keyframe now decodes once, onto the exact output canvas, in a pre-loop that owns every FIRST/LAST_FRAME part: the pixels fill enc_pixels for the VAE pin as before, and the frame itself rides to the prompt block, where the anchor loop hands it to the tower through the new IMultimodal prepare_frame -- implemented by the bridge as Processor::image_from, which smart-resizes and patchifies the caller's pixels exactly as a prepared file's would be. A reference picture is unchanged (tower `match`, encoder grid); the two never share a request. The encode loop's keyframe branch is gone: its pixels were built above, one decode for both consumers. Co-Authored-By: Claude --- core/mm/processor.h | 4 +- core/server/adapters.h | 12 ++++++ core/server/iface.h | 13 +++++++ core/server/videos.cpp | 88 +++++++++++++++++++++++++++++------------- tests/server_fakes.h | 12 ++++++ tests/videos_test.cpp | 19 +++++---- 6 files changed, 114 insertions(+), 34 deletions(-) diff --git a/core/mm/processor.h b/core/mm/processor.h index 14b70f9c..33208dc9 100644 --- a/core/mm/processor.h +++ b/core/mm/processor.h @@ -368,7 +368,9 @@ public: std::string* why, int64_t max_pixels = 0, const ImageMode& mode = ImageMode{}) const; int video(const uint8_t* bytes, size_t n, std::shared_ptr* out, std::string* why) const; - /* From an already-decoded picture or frame list; for tests. */ + /* From an already-decoded picture or frame list: tests feed synthetic frames, and the video + * server feeds the canvas frame of a keyframe so the tower sees the very pixels the video VAE + * is handed (server/iface.h prepare_frame). The smart-resize and patch staging match image(). */ int image_from(const Frame& f, std::shared_ptr* out, std::string* why, const ImageMode& mode = ImageMode{}) const; int video_from(const Video& v, std::shared_ptr* out, std::string* why) const; diff --git a/core/server/adapters.h b/core/server/adapters.h index 347bd82f..7e53ea89 100644 --- a/core/server/adapters.h +++ b/core/server/adapters.h @@ -746,6 +746,18 @@ public: return rc; } + /* The frame arrives already decoded (a keyframe on the output canvas), so no decoder is + * needed -- only the tower the processor patchifies for. It smart-resizes internally, exactly + * as a prepared file would, but from the caller's pixels. */ + int prepare_frame(const mm::Frame& f, std::shared_ptr* out, + std::string* why) override { + if (!accepts(MediaKind::Image)) { + if (why) *why = "this model's vision tower serves no pictures"; + return RAD_E_UNSUPPORTED; + } + return p_->image_from(f, out, why); + } + int encoder_image(const uint8_t* bytes, size_t n, int32_t ratio, int32_t max_side, mm::Frame* out, std::string* why) override { if (!p_ || !mm::decoder_available()) { diff --git a/core/server/iface.h b/core/server/iface.h index 5aed0a0f..b6a529f0 100644 --- a/core/server/iface.h +++ b/core/server/iface.h @@ -568,6 +568,19 @@ public: virtual int prepare(MediaKind k, const std::vector& bytes, const MediaOpts& opts, std::shared_ptr* out, std::string* why) = 0; + /* Process an ALREADY-DECODED frame for the language model's vision tower. The caller built the + * pixels itself -- a keyframe resized to the exact output canvas -- and the tower must see + * exactly those, not a fresh decode of the source file at its own size: the reference shows the + * tower the same resized tensor it hands the video VAE, so an anchor's identity and its pinned + * latent are the same pixels. A server whose tower can read host-built frames implements this; + * the rest leave it unsupported. */ + virtual int prepare_frame(const mm::Frame& f, std::shared_ptr* out, + std::string* why) { + (void)f; (void)out; + if (why) *why = "this server cannot show a pre-decoded frame to the vision tower"; + return RAD_E_UNSUPPORTED; + } + /* A picture in a video VAE's encoder-ready form: decoded under the same guard as prepare() and * resized so both sides are multiples of `ratio` and neither exceeds `max_side`. The video * generator's server uses it to fill a reference's encoder pixels; a server with no video VAE diff --git a/core/server/videos.cpp b/core/server/videos.cpp index 7bdd784b..bbf2a80d 100644 --- a/core/server/videos.cpp +++ b/core/server/videos.cpp @@ -777,6 +777,44 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) { r->refs.insert(r->refs.end(), keys.begin(), keys.end()); const bool has_keys = !keys.empty(); + /* A keyframe decodes ONCE, onto the output canvas, and both of its consumers take their share + * of that single frame: the VAE pin (enc_pixels, staged into the encode pass below) and the + * vision tower (prepare_frame, in the prompt block). The reference hands the tower the same + * resized tensor it hands the vae.encode — the same pixels, never a second decode of the + * source file at its own size — so the identity the tower reads is the identity that gets + * pinned. The count check already required a model that encodes, and a keyframe never shares a + * request with reference pictures, so this loop owns every FIRST/LAST_FRAME part's pixels. + * `canvas` parallels `r->refs`; a zero side marks "no canvas frame" (only when the server + * carries no media at all, where the tower sees nothing either). */ + std::vector canvas; + if (has_keys && info.encodes && deps_.mm) { + canvas.resize(r->refs.size()); + for (size_t i = 0; i < r->refs.size(); ++i) { + gen::VideoRefMedia& m = r->refs[i]; + if (m.kind != gen::VideoRefMedia::FIRST_FRAME && + m.kind != gen::VideoRefMedia::LAST_FRAME) continue; + /* Resized to the EXACT output canvas: the first a plain stretch ("disabled" -- the + * reference's own resize) unless the request asked first_frame_mode=center, the last + * always a centre crop ("center"). Its latent grid is then the target's, so the pack's + * pinned rows (frame_rows wide) match what the encoder writes into the image slot. */ + mm::Frame fr; + std::string why; + if (deps_.mm->encoder_image_canvas((const uint8_t*)m.data.data(), m.data.size(), + (int32_t)W, (int32_t)H, + m.kind == gen::VideoRefMedia::LAST_FRAME + || first_center, + &fr, &why) < 0) + return bad(m.name.c_str(), "'" + m.name + "' could not be encoded: " + why); + m.enc_frames = 1; + m.enc_h = fr.h; + m.enc_w = fr.w; + m.enc_pixels.resize((size_t)fr.w * fr.h * 3); + for (size_t k = 0; k < m.enc_pixels.size(); ++k) + m.enc_pixels[k] = (float)fr.rgb[k]; + canvas[i] = std::move(fr); + } + } + /* ---- 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 * after its soundtrack's label, then the standalone audios). Each `: ` label is @@ -815,20 +853,33 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) { 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 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. */ + * ADDITION to the VAE-latent pin its canvas frame took from the pre-loop above — the + * reference's own split (clip.tokenize + vae.encode). The two channels of a keyframe are + * the SAME pixels: the tower reads the canvas frame itself (prepare_frame), never a second + * decode of the file at the match size `mo`. 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) { + auto keyframe = [](const gen::VideoRefMedia& m) { + return m.kind == gen::VideoRefMedia::FIRST_FRAME || + m.kind == gen::VideoRefMedia::LAST_FRAME; + }; + for (size_t i = 0; i < r->refs.size(); ++i) { + const gen::VideoRefMedia& m = r->refs[i]; if (!anchor(m)) continue; - const std::vector bytes(m.data.begin(), m.data.end()); std::shared_ptr it; std::string why; - if (deps_.mm->prepare(MediaKind::Image, bytes, mo, &it, &why) < 0 || !it) + int rc; + if (keyframe(m) && !canvas.empty() && canvas[i].w > 0) + rc = deps_.mm->prepare_frame(canvas[i], &it, &why); + else { + const std::vector bytes(m.data.begin(), m.data.end()); + rc = deps_.mm->prepare(MediaKind::Image, bytes, mo, &it, &why); + } + if (rc < 0 || !it) return bad(m.name.c_str(), "'" + m.name + "' could not be used: " + why); pics.push_back(std::move(it)); } @@ -927,7 +978,8 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) { /* ---- the encoder's own view of every reference the VAEs consume: pictures and videos through * the video VAE (enc_pixels), soundtracks and standalone audios through the audio VAE - * (enc_samples). The text encoder saw the pictures above for identity; now each reference is + * (enc_samples); keyframes are absent, the canvas pre-loop already built their one frame. The + * text encoder saw the pictures above for identity; now each reference is * decoded to the encoder's own grid so the driver can stage it into a Vencode or Aencode pass. * These are the NATURAL decoded shapes — the driver's encode pass, which owns the encoder's * geometry, clip-pads a video's frames and zero-pads an audio's samples to what it reports. The @@ -939,25 +991,9 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) { const uint8_t* p = (const uint8_t*)m.data.data(); const size_t n = m.data.size(); if (m.kind == gen::VideoRefMedia::FIRST_FRAME || - m.kind == gen::VideoRefMedia::LAST_FRAME) { - /* A keyframe anchor resized to the EXACT output canvas: the first a plain stretch - * ("disabled" -- the reference's own resize) unless the request asked - * first_frame_mode=center, the last always a centre crop ("center"). Its latent - * grid is then the target's, so the pack's pinned rows (frame_rows wide) match - * what the encoder writes into the image slot. */ - mm::Frame fr; - if (deps_.mm->encoder_image_canvas(p, n, (int32_t)W, (int32_t)H, - m.kind == gen::VideoRefMedia::LAST_FRAME - || first_center, - &fr, &why) < 0) - return bad(m.name.c_str(), "'" + m.name + "' could not be encoded: " + why); - m.enc_frames = 1; - m.enc_h = fr.h; - m.enc_w = fr.w; - m.enc_pixels.resize((size_t)fr.w * fr.h * 3); - for (size_t i = 0; i < m.enc_pixels.size(); ++i) - m.enc_pixels[i] = (float)fr.rgb[i]; - } else if (m.kind == gen::VideoRefMedia::IMAGE) { + m.kind == gen::VideoRefMedia::LAST_FRAME) + continue; /* the canvas pre-loop built its pixels — one decode, two consumers */ + if (m.kind == gen::VideoRefMedia::IMAGE) { mm::Frame fr; if (deps_.mm->encoder_image(p, n, (int32_t)info.enc_ratio, (int32_t)info.max_enc_side, &fr, &why) < 0) diff --git a/tests/server_fakes.h b/tests/server_fakes.h index eeb7fff7..d82aeadf 100644 --- a/tests/server_fakes.h +++ b/tests/server_fakes.h @@ -405,6 +405,18 @@ struct FakeMultimodal : IMultimodal { *out = std::move(it); return RAD_OK; } + /* The keyframe path shows the tower the canvas frame itself, not the file: counted, with each + * frame's side, so a test can confirm the tower saw the very pixels the VAE was handed. */ + int n_pframe = 0; + std::vector> pframe_wh; + int prepare_frame(const mm::Frame& f, std::shared_ptr* out, std::string*) override { + ++n_pframe; + pframe_wh.emplace_back(f.w, f.h); + auto it = std::make_shared(); + it->expand = expand; + *out = std::move(it); + return RAD_OK; + } /* The video/audio VAE form, which the server fills only when a generator declares `encodes`. * Synthetic frames and a mono tone of a fixed length, each call counted, so a test can watch the * server decode each reference, spread the mono tone across the encoder's channels and hand it to diff --git a/tests/videos_test.cpp b/tests/videos_test.cpp index 787a97dc..f8d6235e 100644 --- a/tests/videos_test.cpp +++ b/tests/videos_test.cpp @@ -643,13 +643,16 @@ TEST(fl2va_keyframes_feed_the_tower_and_pin_the_output_ends) { FormPart{ "last_frame", "l.png", "image/png", "PL" } }; HttpResponse r = f->srv->router().dispatch(q); REQUIRE_EQ(r.status, 200); - /* 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({ 'P', 'F' })); - CHECK(mm.got[1].second == std::vector({ 'P', 'L' })); - /* ... and are BOTH canvas-resized for the VAE pin (the second, independent channel). */ - CHECK_EQ(mm.n_cenc, 2); + /* Both anchors reach the vision tower -- as the CANVAS frames, the same pixels the VAE is + * handed (the reference feeds the tower its resized tensor too), never the files re-decoded + * at their own size. So: no raw-file prepare for keyframes, two canvas frames of the output + * size, and the ONE decode per frame serving both channels. */ + CHECK(mm.got.empty()); + REQUIRE_EQ(mm.n_pframe, 2); + REQUIRE_EQ(mm.pframe_wh.size(), (size_t)2); + CHECK(mm.pframe_wh[0] == std::make_pair((int32_t)256, (int32_t)256)); + CHECK(mm.pframe_wh[1] == std::make_pair((int32_t)256, (int32_t)256)); + CHECK_EQ(mm.n_cenc, 2); /* one canvas-resize per frame, pin AND tower */ 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); @@ -711,6 +714,8 @@ TEST(l2va_presents_its_sole_frame_as_picture_1) { CHECK_EQ((int)gen.last().refs[0].kind, (int)gen::VideoRefMedia::LAST_FRAME); CHECK_EQ(gen.last().refs[0].index, 1); /* the sole picture: , not the role's 2 */ CHECK(mm.cenc_cover); /* it still takes the last frame's centre crop */ + CHECK(mm.got.empty()); /* the tower reads the canvas frame, not the file */ + CHECK_EQ(mm.n_pframe, 1); REQUIRE(gen.last().media != nullptr); REQUIRE_EQ(gen.last().media->items.size(), (size_t)1); std::vector want;