Archived
video: endpoint keyframe completion (I2VA / L2VA / FL2VA)
One keyframe path serves three DaSiWa modes: first_frame pins output frame 0,
last_frame pins the last frame, and the gap is diffused. A keyframe is pure
attention conditioning -- its VAE latent rides as a pinned (update=0) cond row
at its frame's time position, faithful to comfy/ldm/minimax/model.py where
keyframe latents are img_update==False rows. The sampler is untouched; the step
merely widens to carry the extra cond rows.
- server (videos.cpp/iface.h/adapters.h): first_frame/last_frame form parts ->
VideoRefMedia::FIRST_FRAME/LAST_FRAME; kf_task validation (i2va/fl2va need
first, l2va/fl2va need last); keyframes are mutually exclusive with refs;
encoder_image_canvas(cover=LAST) sizes the anchor.
- driver (video_driver.cpp): keyframe pass issues a RAD_ENC_IMAGE Vencode per
anchor (slot = ++ref_tab[1], ordered), appends ref_tab key triples
{frame(0|frames-1), video_t=1, audio_t=0}.
- arch (minimaxh3.cpp): ref_parts emits keyframe cond parts FIRST; the step
scatter admits kR2VCond/kR2VCondAudio rows.
- tests: video_gen_test keyframe-completes-widens-the-step-and-pins-the-ends
(2x Vencode at slot 1,2; widened step; seg_cu/lat_cu/ref_tab pinned); fake_pack_ref
lays keyframe rows (nk=0 byte-identical to before); videos_test keyframe API.
Device-verified (TP2, seed 9999): FL2VA completes a valid 73-frame MP4+AAC; pins
are consumed (fl2va != same-seed t2va) and force the endpoints (frame0 vs anchor
0.687 pinned vs 0.460 un-pinned; last 0.719 vs 0.521). Fidelity follow-up (not
function): anchors ride as pins only; the reference also feeds them through the
TE as vision tokens, so a prompt that contradicts the anchor lets text win --
full endpoint parity vs a golden needs TE-anchor tokens (#10/P6 bucket).
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
8ddcbbf01f
commit
60e920cd4d
11 files changed
+410
-37
No files matched your search
@@ -3080,15 +3080,27 @@ static minimaxh3::Ref2VAPack ref_pack_of(const RadVideoGeom* g, const int32_t* r
|
||||
/* Which encoder slot each reference part's latents sit in, in the order ref2va_build lays the
|
||||
* reference segments down -- a standalone soundtrack alone; a clip its soundtrack (when it carries
|
||||
* one) then its video; a picture its video alone. The encode pass wrote these into b_evc / b_enca at
|
||||
* the same slot it names here (the driver's queue uses the ref's own index). Keyframe (cond) rows
|
||||
* ride the continuation tail, not an encode, so they are absent -- and get no scatter until the tail
|
||||
* path lands. `audio` picks the buffer (1 = b_enca, 0 = b_evc); `row` is that buffer's base for the
|
||||
* the same slot it names here (the driver's queue uses the ref's own index). A keyframe (cond) row is
|
||||
* encoded the same way a reference is -- a picture anchor rides the image slot its index names, a
|
||||
* clip anchor's soundtrack the audio slot -- so it takes a part ahead of the references, whose
|
||||
* segments ref2va_build lays after the keyframes'. `audio` picks the buffer (1 = b_enca, 0 = b_evc); `row` is that buffer's base for the
|
||||
* part. A row < 0 means the slot is unknown -- a table the encode queue never filled. */
|
||||
struct RefPart { int audio; int64_t row; };
|
||||
static void ref_parts(const int32_t* ref_tab, std::vector<RefPart>* out) {
|
||||
if (!ref_tab) return;
|
||||
const int32_t nr = ref_tab[0];
|
||||
const int32_t nr = ref_tab[0], nk = ref_tab[1];
|
||||
const int32_t* w = ref_tab + 2;
|
||||
/* Keyframe parts FIRST: ref2va_build lays the sequence [text | keyframes | refs | targets], and
|
||||
* the scatter consumes parts in segment order, so a keyframe's part precedes every reference's.
|
||||
* A keyframe's video latent rides the image slot the key's own index names -- a keyframe request
|
||||
* carries no references, so the image slots are free for it -- and a picture anchor's soundtrack
|
||||
* (absent for FL2VA's stills, present for a clip keyframe) the standalone-audio slot. `w` stays
|
||||
* where the reference loop below needs it; the keys trail the references in the table. */
|
||||
const int32_t* kw = ref_tab + 2 + (int64_t)nr * 6;
|
||||
for (int32_t i = 0; i < nk; ++i, kw += 3) {
|
||||
if (kw[1] > 0) out->push_back({ 0, venc_slot_row(RAD_ENC_IMAGE, i + 1) });
|
||||
if (kw[2] > 0) out->push_back({ 1, aenc_slot_row(RAD_ENC_AUDIO, i + 1) });
|
||||
}
|
||||
for (int32_t i = 0; i < nr; ++i, w += 6) {
|
||||
const int32_t kind = w[0], slot = w[1], aframes = w[5];
|
||||
if (kind == minimaxh3::kR2VRefAudioOnly) {
|
||||
@@ -3313,10 +3325,11 @@ static void step_dit(RadCtx* c, const RadBatch* batch, const Model& m) {
|
||||
RAD_W(m.w_ain), RAD_W(m.b_ain), RAD_NONE, brow_slice(m.b_h, aseg, arows, D));
|
||||
RAD_ISSUE_N(c, m.op_vin, vrows, brow_slice(m.b_vstate, lane * m.maxV, vrows, kPatchVals),
|
||||
RAD_W(m.w_vin), RAD_W(m.b_vin), RAD_NONE, brow_slice(m.b_h, vseg, vrows, D));
|
||||
/* Then each reference block: the same input projection, but its latents read from the encoder's
|
||||
* slot (b_evc / b_enca), scattered into the rows the pack placed between the prompt and the
|
||||
* targets. Every ref part -- a picture, a clip's video, a clip's or a standalone's soundtrack --
|
||||
* is one issue, its part list consumed in the order ref2va_build laid the segments down. */
|
||||
/* Then each conditioning block -- a keyframe's pinned rows, then every reference: the same input
|
||||
* projection, but its latents read from the encoder's slot (b_evc / b_enca), scattered into the
|
||||
* rows the pack placed between the prompt and the targets. Every part -- a keyframe's picture
|
||||
* anchor, a picture, a clip's video, a clip's or a standalone's soundtrack -- is one issue, its
|
||||
* part list consumed in the order ref2va_build laid the segments down (keyframes first). */
|
||||
if (refc) {
|
||||
std::vector<RefPart> parts;
|
||||
ref_parts(V->ref_tab, &parts);
|
||||
@@ -3324,7 +3337,10 @@ static void step_dit(RadCtx* c, const RadBatch* batch, const Model& m) {
|
||||
for (const minimaxh3::Ref2VASeg& s : pk.segs) {
|
||||
const bool img = minimaxh3::ref2va_is_img(s.kind), aud = minimaxh3::ref2va_is_audio(s.kind);
|
||||
if (!img && !aud) continue; /* text, and the targets above */
|
||||
if (s.kind != minimaxh3::kR2VRefImg && s.kind != minimaxh3::kR2VRefAudio) continue;
|
||||
/* A keyframe's pinned rows (kR2VCond / kR2VCondAudio) scatter exactly as a reference's
|
||||
* do -- same projection, same encoder slot -- so they join the references here. */
|
||||
if (s.kind != minimaxh3::kR2VRefImg && s.kind != minimaxh3::kR2VRefAudio &&
|
||||
s.kind != minimaxh3::kR2VCond && s.kind != minimaxh3::kR2VCondAudio) continue;
|
||||
if (pi >= parts.size()) {
|
||||
rad_step_fail(c, "minimaxh3: the reference conditioning has more reference blocks "
|
||||
"than its table describes");
|
||||
|
||||
+8
-1
@@ -79,7 +79,14 @@ private:
|
||||
* its kind, which fixes its slot in the conditioning; VIDEO_AUDIO #i is the soundtrack of
|
||||
* VIDEO #i. */
|
||||
struct VideoRefMedia {
|
||||
enum Kind : int32_t { IMAGE = 0, VIDEO = 1, VIDEO_AUDIO = 2, AUDIO = 3 };
|
||||
/* FIRST_FRAME / LAST_FRAME are endpoint KEYFRAMES, not references: a keyframe completion pins
|
||||
* one output frame (first to frame 0, last to the final frame) and diffuses the gap between.
|
||||
* They are never conditioned on as identities, so they take no reference row -- the driver folds
|
||||
* 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. */
|
||||
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;
|
||||
std::string name, filename, content_type; /* the part's name and what the client called the file */
|
||||
|
||||
@@ -417,12 +417,14 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
|
||||
* frames and its soundtrack's are the Vencode/Aencode latent counts rad_arch_encode_geom gives
|
||||
* (a video's are the same before and after the encode pass clip-pads its short final clip). The
|
||||
* table must exist before the step layout is decided, because when the denoiser consumes it the
|
||||
* reference rows are part of that layout. The keyframe count stays 0 (continuation is separate
|
||||
* work), so the table holds only reference rows. */
|
||||
* reference rows are part of that layout. Endpoint keyframes (a first and/or last frame pinned
|
||||
* at the output's ends) are appended after the reference rows as {frame, video_t, audio_t}
|
||||
* triples; a request carries either references or keyframes, never both. */
|
||||
if (d.encodes) {
|
||||
bool need_geom = false;
|
||||
for (const VideoRefMedia& m : r->refs)
|
||||
if (m.kind != VideoRefMedia::IMAGE) need_geom = true;
|
||||
if (m.kind != VideoRefMedia::IMAGE && m.kind != VideoRefMedia::FIRST_FRAME &&
|
||||
m.kind != VideoRefMedia::LAST_FRAME) need_geom = true;
|
||||
if (need_geom && !enc_geom_)
|
||||
return no("the request carries a reference video or audio, but this plugin exports no "
|
||||
"rad_arch_encode_geom, so their encoder geometry cannot be sized");
|
||||
@@ -507,6 +509,33 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
|
||||
}
|
||||
/* VIDEO_AUDIO rows are folded into their video above, so they take no row here. */
|
||||
}
|
||||
/* Endpoint keyframes: each FIRST_FRAME/LAST_FRAME the server resized to the target canvas
|
||||
* and staged in enc_pixels folds into the pack's KEYFRAME table -- not a reference row. The
|
||||
* DiT reads its latent rows PINNED at that frame's moment on the target timeline (denoised
|
||||
* never): the first pins output frame 0, the last the final frame, and the gap between is
|
||||
* diffused. The key triple is {pixel frame, video_t, audio_t}; a picture anchor is video-only
|
||||
* (video_t = one latent frame, audio_t = 0), and its pixel frame is 0 or frames-1 -- the same
|
||||
* resolved frame index the reference's MiniMaxH3ImageToVideo assigns. Its latents ride the
|
||||
* image slot its keyframe ORDER names (1, then 2): a keyframe request carries no references,
|
||||
* so those slots are free, and ref_parts -- and the scatter -- read them back by the same
|
||||
* order. Keyframe rows are appended after every reference row, which is where ref_pack_of
|
||||
* reads the table's key block. */
|
||||
for (size_t i = 0; i < r->refs.size(); ++i) {
|
||||
const VideoRefMedia& m = r->refs[i];
|
||||
const bool first = m.kind == VideoRefMedia::FIRST_FRAME;
|
||||
if (!first && m.kind != VideoRefMedia::LAST_FRAME) continue;
|
||||
if (m.enc_pixels.empty())
|
||||
return no("'" + m.name + "' was not prepared for the video encoder");
|
||||
if (bad_side(m))
|
||||
return no("'" + m.name + "' has encoder sides (" + std::to_string(m.enc_h) +
|
||||
"x" + std::to_string(m.enc_w) + ") the VAE grid of " +
|
||||
std::to_string(d.enc_ratio) + " does not divide");
|
||||
if (j->ref_tab.empty()) j->ref_tab.assign(2, 0); /* [n_refs, n_keys] */
|
||||
const int32_t slot = ++j->ref_tab[1]; /* keyframe order -> image slot 1, 2 */
|
||||
j->enc_q.push_back({RAD_ENC_IMAGE, slot, (int32_t)i});
|
||||
j->ref_tab.insert(j->ref_tab.end(),
|
||||
{first ? 0 : s.frames - 1, 1 /*one latent frame*/, 0 /*no soundtrack*/});
|
||||
}
|
||||
}
|
||||
/* The step's packed sequence. When the denoiser consumes the reference table the pack is the
|
||||
* only source of its rows, segment boundaries and positions -- the reference rows sit between
|
||||
@@ -640,8 +669,8 @@ int VideoDriver::submit(std::unique_ptr<VideoRequest> r, std::string* why) {
|
||||
* chosen. Each reference the server decoded into encoder form rides one encode pass at the slot
|
||||
* its index numbers within its kind -- a picture or video a Vencode pass, a standalone audio and
|
||||
* every video's soundtrack an Aencode pass (a soundtrack at its video's slot, since the encoder
|
||||
* and the scatter both number it by its video). The keyframe count stays 0: continuation is
|
||||
* separate work. */
|
||||
* and the scatter both number it by its video). Endpoint keyframes ride the same queue as
|
||||
* picture anchors -- a Vencode pass at the image slot their keyframe order names. */
|
||||
/* The soundtrack's length, exactly the samples the decode will hand the encoder — the
|
||||
* number a caller that chains clips against an audio floor checks. A build without
|
||||
* libavcodec never gets the muxed file to read it back from, so it is logged here. */
|
||||
|
||||
@@ -1041,6 +1041,44 @@ int Processor::encoder_image(const uint8_t* bytes, size_t n, int32_t ratio, int3
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
int Processor::encoder_image_canvas(const uint8_t* bytes, size_t n, int32_t w, int32_t h,
|
||||
bool cover, Frame* out, std::string* why) const {
|
||||
if (w < 1 || h < 1) {
|
||||
if (why) *why = "the keyframe canvas is empty";
|
||||
return RAD_E_INVAL;
|
||||
}
|
||||
/* The same decode guard as encoder_image(): a keyframe the client sends is held to the size a
|
||||
* reference would be. */
|
||||
Frame f;
|
||||
RAD_TRY(decode_image(bytes, n, cfg_.max_source_pixels, &f, why));
|
||||
if (!cover) {
|
||||
/* "disabled": a plain stretch to the canvas, exactly the reference's first-frame resize. */
|
||||
if (w == f.w && h == f.h) { *out = std::move(f); return RAD_OK; }
|
||||
resize_lanczos_rgb(f, w, h, out);
|
||||
return RAD_OK;
|
||||
}
|
||||
/* "center": scale up to COVER the canvas keeping aspect (both axes at least the target), then
|
||||
* cut the exact canvas from the centre -- the reference's last-frame resize. The scale is the
|
||||
* larger of the two axis ratios so neither side falls short; each is rounded up to at least the
|
||||
* target so the crop always has enough to take from. */
|
||||
const double s = std::max((double)w / (double)f.w, (double)h / (double)f.h);
|
||||
int32_t cw = (int32_t)std::llround((double)f.w * s);
|
||||
int32_t ch = (int32_t)std::llround((double)f.h * s);
|
||||
if (cw < w) cw = w;
|
||||
if (ch < h) ch = h;
|
||||
Frame big;
|
||||
resize_lanczos_rgb(f, cw, ch, &big);
|
||||
const int32_t x0 = (cw - w) / 2, y0 = (ch - h) / 2;
|
||||
out->w = w;
|
||||
out->h = h;
|
||||
out->rgb.resize((size_t)w * h * 3);
|
||||
for (int32_t y = 0; y < h; ++y)
|
||||
for (int32_t x = 0; x < w * 3; ++x)
|
||||
out->rgb[(size_t)y * w * 3 + x] =
|
||||
big.rgb[(size_t)(y0 + y) * cw * 3 + (size_t)(x0 * 3 + x)];
|
||||
return RAD_OK;
|
||||
}
|
||||
|
||||
namespace {
|
||||
/* Which frames of a source video a reference clip keeps: spread uniformly over the stream, the
|
||||
* largest count no more than both the stream's length and `max_frames` that is 5 modulo 17 — the
|
||||
|
||||
@@ -327,6 +327,17 @@ public:
|
||||
int encoder_image(const uint8_t* bytes, size_t n, int32_t ratio, int32_t max_side, Frame* out,
|
||||
std::string* why) const;
|
||||
|
||||
/* A keyframe anchor in the video VAE's own form: decoded under the same `max_source_pixels`
|
||||
* guard, then resized to the EXACT output canvas (`w` x `h`), unlike `encoder_image`'s
|
||||
* aspect-kept snap. A keyframe's pinned rows must cover the target's latent grid exactly -- it
|
||||
* is frame_rows wide, not the anchor's own grid -- so the anchor cannot keep its aspect: the
|
||||
* reference stretches the first frame to the canvas ("disabled") and cover-crops the last
|
||||
* ("center"). `cover` picks between them: false stretches both axes to fill the canvas (may
|
||||
* distort); true instead scales up to COVER the canvas keeping aspect, then centre-crops
|
||||
* the excess. `w`/`h` are the request's target size, already a multiple of the VAE ratio. */
|
||||
int encoder_image_canvas(const uint8_t* bytes, size_t n, int32_t w, int32_t h, bool cover,
|
||||
Frame* out, std::string* why) const;
|
||||
|
||||
/* A moving picture in the video VAE's own form: decoded under the same `max_source_pixels`
|
||||
* guard, then every frame resized to the one grid `encoder_image` picks. The frames are
|
||||
* SAMPLED first: uniformly across the stream, to the largest count no more than `max_frames`
|
||||
|
||||
@@ -719,6 +719,15 @@ public:
|
||||
return p_->encoder_image(bytes, n, ratio, max_side, out, why);
|
||||
}
|
||||
|
||||
int encoder_image_canvas(const uint8_t* bytes, size_t n, int32_t w, int32_t h, bool cover,
|
||||
mm::Frame* out, std::string* why) override {
|
||||
if (!p_ || !mm::decoder_available()) {
|
||||
if (why) *why = "no picture decoder is available";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
return p_->encoder_image_canvas(bytes, n, w, h, cover, out, why);
|
||||
}
|
||||
|
||||
int encoder_video(const uint8_t* bytes, size_t n, int32_t ratio, int32_t max_side,
|
||||
int32_t max_frames, std::vector<mm::Frame>* out, std::string* why) override {
|
||||
if (!p_ || !mm::decoder_available()) {
|
||||
|
||||
@@ -544,6 +544,17 @@ public:
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
/* A keyframe anchor in a video VAE's encoder-ready form: decoded under the same guard, then
|
||||
* resized to the EXACT `w` x `h` output canvas (`cover` selects a plain stretch or a centre
|
||||
* crop), not an aspect-kept snap -- a pinned keyframe's latent grid must be the target's. The
|
||||
* video generator's server uses it for first_frame/last_frame parts. */
|
||||
virtual int encoder_image_canvas(const uint8_t* bytes, size_t n, int32_t w, int32_t h,
|
||||
bool cover, mm::Frame* out, std::string* why) {
|
||||
(void)bytes; (void)n; (void)w; (void)h; (void)cover; (void)out;
|
||||
if (why) *why = "this server prepares no reference media for a video encoder";
|
||||
return RAD_E_UNSUPPORTED;
|
||||
}
|
||||
|
||||
/* A moving picture in a video VAE's encoder-ready form: decoded under the same guard, sampled to
|
||||
* a clip-friendly frame count (at most `max_frames`) and resized to `encoder_image`'s grid. The
|
||||
* frames are the trimmed, sampled count; the caller clip-pads to a whole clip. */
|
||||
|
||||
+89
-7
@@ -272,6 +272,11 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
* since it may carry a file). A form may carry the reference media of a ref2va request as
|
||||
* named file parts; they are collected here and checked once the task is known. */
|
||||
std::vector<gen::VideoRefMedia> refs;
|
||||
/* Endpoint keyframes: the "first_frame"/"last_frame" file parts, kept apart from the reference
|
||||
* media because they are conditioned on differently (pinned rows, not reference rows) and are
|
||||
* validated and encoded on their own path below. At most one of each; together they are FL2VA,
|
||||
* alone they are I2VA (first) or L2VA (last). */
|
||||
std::vector<gen::VideoRefMedia> keys;
|
||||
json body = json::object();
|
||||
if (!req.form.empty()) {
|
||||
for (const FormPart& p : req.form) {
|
||||
@@ -281,6 +286,21 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
"ref_image_N file parts"
|
||||
: "this server makes video from text "
|
||||
"only"), req.peer);
|
||||
if (p.name == "first_frame" || p.name == "last_frame") {
|
||||
if (p.filename.empty())
|
||||
return error_response(err_bad(p.name, "'" + p.name + "' must carry an image "
|
||||
"file"), req.peer);
|
||||
gen::VideoRefMedia m;
|
||||
m.kind = p.name == "first_frame" ? gen::VideoRefMedia::FIRST_FRAME
|
||||
: gen::VideoRefMedia::LAST_FRAME;
|
||||
m.index = 1;
|
||||
m.name = p.name;
|
||||
m.filename = p.filename;
|
||||
m.content_type = p.content_type;
|
||||
m.data = p.data;
|
||||
keys.push_back(std::move(m));
|
||||
continue;
|
||||
}
|
||||
if (!p.filename.empty()) {
|
||||
gen::VideoRefMedia m;
|
||||
if (!parse_ref_name(p.name, &m.kind, &m.index))
|
||||
@@ -341,16 +361,26 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
req.peer);
|
||||
/* The task: text always; reference conditioning for a model that takes reference media. */
|
||||
bool ref2va = false;
|
||||
std::string kf_task; /* an explicitly named keyframe task (i2va / l2va / fl2va), if any */
|
||||
if (has(body, "task")) {
|
||||
const std::string task = body["task"].is_string() ? body["task"].get<std::string>() : "";
|
||||
if (task == "ref2va" && takes_refs) ref2va = true;
|
||||
/* The keyframe-completion tasks: a first and/or last frame pinned at the output's ends.
|
||||
* They are recognised whatever a model declares (any model with a video encoder can pin a
|
||||
* keyframe); which frames are actually present is decided by the file parts, cross-checked
|
||||
* against this name below. t2va with a first_frame part is I2VA; a named task missing its
|
||||
* frame is refused there. */
|
||||
else if (task == "i2va" || task == "l2va" || task == "fl2va") kf_task = task;
|
||||
else if (task != "t2va") {
|
||||
ApiError e = err_unsupported("task", takes_refs
|
||||
? "this server makes video from text or from reference "
|
||||
"media: t2va, ref2va"
|
||||
: "this server makes video from text: t2va");
|
||||
e.supported = takes_refs ? std::vector<std::string>{ "t2va", "ref2va" }
|
||||
: std::vector<std::string>{ "t2va" };
|
||||
? "this server makes video from text, from reference "
|
||||
"media, or between keyframes: t2va, ref2va, i2va, l2va, "
|
||||
"fl2va"
|
||||
: "this server makes video from text or between keyframes: "
|
||||
"t2va, i2va, l2va, fl2va");
|
||||
e.supported = takes_refs
|
||||
? std::vector<std::string>{ "t2va", "ref2va", "i2va", "l2va", "fl2va" }
|
||||
: std::vector<std::string>{ "t2va", "i2va", "l2va", "fl2va" };
|
||||
return error_response(e, req.peer);
|
||||
}
|
||||
}
|
||||
@@ -369,6 +399,36 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
ApiError e;
|
||||
if (validate_refs(refs, info, &e) < 0) return error_response(e, req.peer);
|
||||
}
|
||||
/* Endpoint keyframes: at most one of each, not alongside reference media (a request either
|
||||
* completes between two pinned frames or conditions on references -- the two conditioning
|
||||
* paths are different rows), and only on a model that encodes (a keyframe reaches the DiT as a
|
||||
* pinned VAE latent). */
|
||||
{
|
||||
int n_first = 0, n_last = 0;
|
||||
for (const gen::VideoRefMedia& m : keys)
|
||||
if (m.kind == gen::VideoRefMedia::FIRST_FRAME) ++n_first; else ++n_last;
|
||||
if (n_first > 1) return bad("first_frame", "at most one first_frame part");
|
||||
if (n_last > 1) return bad("last_frame", "at most one last_frame part");
|
||||
if (!keys.empty()) {
|
||||
if (!info.encodes)
|
||||
return bad("first_frame", "this model has no video encoder, so it cannot pin a "
|
||||
"keyframe");
|
||||
if (!refs.empty())
|
||||
return bad("first_frame", "a keyframe completion conditions on its pinned frames, "
|
||||
"not on reference media: give first_frame/last_frame or "
|
||||
"the ref_* parts, not both");
|
||||
}
|
||||
/* A named keyframe task must carry the frame(s) it promises; t2va with parts is fine, the
|
||||
* parts alone decide the mode. */
|
||||
if (!kf_task.empty()) {
|
||||
if ((kf_task == "i2va" || kf_task == "fl2va") && n_first < 1)
|
||||
return bad("first_frame", "task '" + kf_task + "' pins a first frame: give a "
|
||||
"first_frame part");
|
||||
if ((kf_task == "l2va" || kf_task == "fl2va") && n_last < 1)
|
||||
return bad("last_frame", "task '" + kf_task + "' pins a last frame: give a "
|
||||
"last_frame part");
|
||||
}
|
||||
}
|
||||
json target = json::object();
|
||||
if (has(body, "target")) {
|
||||
if (!body["target"].is_object()) return bad("target", "expected an object");
|
||||
@@ -495,6 +555,11 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
r->seed = (((uint64_t)rd() << 32) ^ rd()) & (((uint64_t)1 << 53) - 1);
|
||||
}
|
||||
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. */
|
||||
r->refs.insert(r->refs.end(), keys.begin(), keys.end());
|
||||
|
||||
/* ---- 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
|
||||
@@ -595,12 +660,29 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
|
||||
* geometry, clip-pads a video's frames and zero-pads an audio's samples to what it reports. The
|
||||
* processor decodes under the same guard it used for the text encoder, so a reference that got
|
||||
* this far decodes here too. */
|
||||
if (ref2va && info.encodes && deps_.mm) {
|
||||
if ((ref2va || !keys.empty()) && info.encodes && deps_.mm) {
|
||||
for (gen::VideoRefMedia& m : r->refs) {
|
||||
std::string why;
|
||||
const uint8_t* p = (const uint8_t*)m.data.data();
|
||||
const size_t n = m.data.size();
|
||||
if (m.kind == gen::VideoRefMedia::IMAGE) {
|
||||
if (m.kind == gen::VideoRefMedia::FIRST_FRAME ||
|
||||
m.kind == gen::VideoRefMedia::LAST_FRAME) {
|
||||
/* A keyframe anchor resized to the EXACT output canvas: first a plain stretch
|
||||
* ("disabled"), last a centre crop ("center") -- the reference's own resize. 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,
|
||||
&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) {
|
||||
mm::Frame fr;
|
||||
if (deps_.mm->encoder_image(p, n, (int32_t)info.enc_ratio,
|
||||
(int32_t)info.max_enc_side, &fr, &why) < 0)
|
||||
|
||||
+12
-1
@@ -406,7 +406,10 @@ struct FakeMultimodal : IMultimodal {
|
||||
* clip, a sample count that is not a whole hop) — padding is the driver's, not the server's. */
|
||||
int32_t enc_frames = 5, enc_side = 16;
|
||||
int64_t enc_n = 850;
|
||||
int n_ienc = 0, n_venc = 0, n_aenc = 0;
|
||||
int n_ienc = 0, n_venc = 0, n_aenc = 0, n_cenc = 0;
|
||||
/* The canvas an anchor was resized to, and whether the last frame's cover-crop was asked for. */
|
||||
int32_t cenc_w = 0, cenc_h = 0;
|
||||
bool cenc_cover = false;
|
||||
int encoder_image(const uint8_t*, size_t, int32_t, int32_t, mm::Frame* out,
|
||||
std::string*) override {
|
||||
++n_ienc;
|
||||
@@ -414,6 +417,14 @@ struct FakeMultimodal : IMultimodal {
|
||||
out->rgb.assign((size_t)enc_side * enc_side * 3, 128);
|
||||
return RAD_OK;
|
||||
}
|
||||
int encoder_image_canvas(const uint8_t*, size_t, int32_t w, int32_t h, bool cover,
|
||||
mm::Frame* out, std::string*) override {
|
||||
++n_cenc;
|
||||
cenc_w = w; cenc_h = h; cenc_cover = cover;
|
||||
out->w = w; out->h = h;
|
||||
out->rgb.assign((size_t)w * h * 3, 100);
|
||||
return RAD_OK;
|
||||
}
|
||||
int encoder_video(const uint8_t*, size_t, int32_t, int32_t, int32_t, std::vector<mm::Frame>* out,
|
||||
std::string*) override {
|
||||
++n_venc;
|
||||
|
||||
+106
-13
@@ -140,11 +140,13 @@ int64_t fake_pack_more(const RadVideoGeom* g, const int32_t* ref_tab, int32_t* s
|
||||
}
|
||||
|
||||
/* The reference-aware layout: when a reference table rides, lay out
|
||||
* [text | one block per reference | audio target | video target] -- boundaries are segment starts
|
||||
* with the total rows last, so the video target runs [seg[nseg-2], seg[nseg-1]). Each picture
|
||||
* contributes a fixed 4 rows: this fake lays out the structure the driver stages, not the
|
||||
* reference's real visual-token arithmetic. With no table it lays the plain sequence, so the
|
||||
* encode/decode passes that call the pack without references get their plain table. */
|
||||
* [text | one block per keyframe | one block per reference | audio target | video target] --
|
||||
* boundaries are segment starts with the total rows last, so the video target runs
|
||||
* [seg[nseg-2], seg[nseg-1]). Keyframes precede references, matching the real pack. Each keyframe
|
||||
* contributes a fixed 2 rows and each picture a fixed 4: this fake lays out the structure the
|
||||
* driver stages, not the reference's real visual-token arithmetic. With no table it lays the plain
|
||||
* sequence, so the encode/decode passes that call the pack without references get their plain
|
||||
* table. With nk = 0 the layout is exactly the reference-only one the older tests assert. */
|
||||
int64_t fake_pack_ref(const RadVideoGeom* g, const int32_t* ref_tab, int32_t* seg, int64_t cap_seg,
|
||||
float* pos, int64_t cap_pos) {
|
||||
const int64_t audio = 2 * (int64_t)g->n_audio;
|
||||
@@ -164,16 +166,18 @@ int64_t fake_pack_ref(const RadVideoGeom* g, const int32_t* ref_tab, int32_t* se
|
||||
return rows;
|
||||
}
|
||||
const int32_t nr = ref_tab[0], nk = ref_tab[1];
|
||||
const int64_t rper = 4;
|
||||
const int64_t rows = g->n_text + (int64_t)nr * rper + audio + vtarget;
|
||||
const int64_t nseg = (int64_t)nr + 4;
|
||||
const int64_t rper = 4, kper = 2;
|
||||
const int64_t rows = g->n_text + (int64_t)nk * kper + (int64_t)nr * rper + audio + vtarget;
|
||||
const int64_t nseg = (int64_t)nk + (int64_t)nr + 4;
|
||||
if (!seg) return rows;
|
||||
if (cap_seg < nseg || nk != 0) return RAD_E_INVAL;
|
||||
if (cap_seg < nseg) return RAD_E_INVAL;
|
||||
seg[0] = 0;
|
||||
for (int32_t r = 0; r < nr; ++r) seg[1 + r] = (int32_t)(g->n_text + (int64_t)r * rper);
|
||||
seg[1 + nr] = (int32_t)(g->n_text + (int64_t)nr * rper); /* audio target start */
|
||||
seg[2 + nr] = (int32_t)(seg[1 + nr] + audio); /* video target start */
|
||||
seg[3 + nr] = (int32_t)(seg[2 + nr] + vtarget); /* total rows */
|
||||
int64_t cur = g->n_text;
|
||||
for (int32_t k = 0; k < nk; ++k) { seg[1 + k] = (int32_t)cur; cur += kper; }
|
||||
for (int32_t r = 0; r < nr; ++r) { seg[1 + nk + r] = (int32_t)cur; cur += rper; }
|
||||
seg[1 + nk + nr] = (int32_t)cur; /* audio target start */
|
||||
seg[2 + nk + nr] = (int32_t)(cur + audio); /* video target start */
|
||||
seg[3 + nk + nr] = (int32_t)(cur + audio + vtarget); /* total rows */
|
||||
if (pos) {
|
||||
if (cap_pos < 3 * rows) return RAD_E_INVAL;
|
||||
for (int64_t c = 0; c < 3; ++c)
|
||||
@@ -892,6 +896,95 @@ TEST(a_reference_conditioned_step_widens_to_the_pack_layout) {
|
||||
CHECK(saw_encode);
|
||||
}
|
||||
|
||||
/* An endpoint-keyframe completion (I2VA/L2VA/FL2VA) rides the same machinery a reference does, but
|
||||
* fills the pack's KEYFRAME table instead of its reference rows. Each first/last anchor the server
|
||||
* resized to the target canvas folds into a Vencode pass at the IMAGE slot its keyframe ORDER names
|
||||
* (1, then 2 -- free, since a keyframe request carries no references), and the step widens to
|
||||
* [text | keyframe block | keyframe block | audio target | video target]. The table is {0 references,
|
||||
* 2 keys} then the two key triples {pixel frame, video_t, audio_t}: the first pins pixel frame 0,
|
||||
* the last the final frame, each one latent frame of video and no soundtrack -- the resolved frame
|
||||
* index the reference's MiniMaxH3ImageToVideo assigns. The sampler still owns only the two target
|
||||
* streams; the pinned rows are never sampled. */
|
||||
TEST(a_keyframe_completes_widen_the_step_and_pin_the_ends) {
|
||||
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; // the denoiser consumes the table: turn the widening on
|
||||
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 }; /* 3 text tokens: pins are not references, so no tower run */
|
||||
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);
|
||||
gen::VideoRefMedia last = ref_part(gen::VideoRefMedia::LAST_FRAME, "last_frame", 2);
|
||||
last.enc_frames = 1;
|
||||
last.enc_h = 16;
|
||||
last.enc_w = 16;
|
||||
last.enc_pixels.assign(16 * 16 * 3, 2.0f);
|
||||
r->refs.push_back(first);
|
||||
r->refs.push_back(last);
|
||||
CHECK_OK(f.drv.submit(std::move(r), &why));
|
||||
|
||||
const gen::VideoShape s = gen::video_shape(d, 64, 32, 2);
|
||||
const int64_t n_text = 3, audio = 2 * s.n_audio, vtarget = s.video_rows, kper = 2;
|
||||
const int64_t widened = n_text + 2 * kper + audio + vtarget;
|
||||
|
||||
int enc_seen = 0;
|
||||
bool saw_first = false, saw_last = false, saw_step = false;
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
const RadBatch* b = f.drv.next(i);
|
||||
if (!b) break;
|
||||
if (b->gen == RAD_GEN_PASS_VENCODE) {
|
||||
const RadVideoGenBatch* V = b->vgen_b;
|
||||
REQUIRE(V != nullptr);
|
||||
CHECK(V->ref_tab != nullptr); /* the keyframe table rides the encode too */
|
||||
CHECK_EQ(V->enc_kind, (int32_t)RAD_ENC_IMAGE); /* a picture anchor at an image slot */
|
||||
CHECK_EQ(V->enc_slot, (int32_t)(++enc_seen)); /* order 1, then 2 */
|
||||
if (enc_seen == 1) saw_first = true;
|
||||
else if (enc_seen == 2) saw_last = true;
|
||||
} else if (b->gen == RAD_GEN_PASS_VSTEP) {
|
||||
const RadVideoGenBatch* V = b->vgen_b;
|
||||
REQUIRE(V != nullptr);
|
||||
saw_step = true;
|
||||
CHECK_EQ(b->n_tok, widened); /* widened by the two key blocks */
|
||||
CHECK_EQ(V->seg_cu[0], 0);
|
||||
CHECK_EQ(V->seg_cu[1], (int32_t)n_text); /* text | key 0 */
|
||||
CHECK_EQ(V->seg_cu[2], (int32_t)(n_text + kper)); /* key 0 | key 1 */
|
||||
CHECK_EQ(V->seg_cu[3], (int32_t)(n_text + 2 * kper)); /* key 1 | audio target */
|
||||
CHECK_EQ(V->seg_cu[4], (int32_t)(n_text + 2 * kper + audio)); /* audio | video target */
|
||||
CHECK_EQ(V->seg_cu[5], (int32_t)widened); /* video grid ends the row */
|
||||
CHECK_EQ(V->lat_cu[1], (int32_t)vtarget); /* sampler owns the target streams */
|
||||
CHECK_EQ(V->lat_cu[3], (int32_t)audio); /* only, never the pinned rows */
|
||||
/* The keyframe table: {n_refs = 0, n_keys = 2}, then two 3-word key triples. Checked
|
||||
* while the step's job is still alive -- the driver retires a finished job (freeing this
|
||||
* table) on the next() that follows its last pass. */
|
||||
const int32_t* tab = V->ref_tab;
|
||||
REQUIRE(tab != nullptr);
|
||||
CHECK_EQ(tab[0], 0); /* no references */
|
||||
CHECK_EQ(tab[1], 2); /* two endpoint keys */
|
||||
CHECK_EQ(tab[2], 0); /* first pins output frame 0 */
|
||||
CHECK_EQ(tab[3], 1); /* one latent frame of video */
|
||||
CHECK_EQ(tab[4], 0); /* no soundtrack */
|
||||
CHECK_EQ(tab[5], s.frames - 1); /* last pins the final frame */
|
||||
CHECK_EQ(tab[6], 1);
|
||||
CHECK_EQ(tab[7], 0);
|
||||
}
|
||||
f.answer(10 * i);
|
||||
CHECK_OK(f.drv.done());
|
||||
}
|
||||
CHECK(saw_first);
|
||||
CHECK(saw_last);
|
||||
CHECK(saw_step);
|
||||
}
|
||||
|
||||
/* 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
|
||||
|
||||
+67
-1
@@ -154,7 +154,9 @@ TEST(a_video_request_that_cannot_be_served_names_its_field) {
|
||||
std::string("num_inference_steps"));
|
||||
CHECK_EQ(v.refused(Videos::body({ { "input_reference", "file-1" } })),
|
||||
std::string("input_reference"));
|
||||
CHECK_EQ(v.refused(Videos::body({ { "task", "fl2va" } })), std::string("task"));
|
||||
/* fl2va is a valid task, but one that promises a first frame -- refused naming the missing
|
||||
* part, not the task. */
|
||||
CHECK_EQ(v.refused(Videos::body({ { "task", "fl2va" } })), std::string("first_frame"));
|
||||
CHECK_EQ(v.refused(Videos::body({ { "conditions", json::array({ json::object() }) } })),
|
||||
std::string("conditions"));
|
||||
CHECK_EQ(v.refused({ { "prompt", "p" }, { "target", { { "aspect_ratio", "wide" } } } }),
|
||||
@@ -512,6 +514,70 @@ TEST(ref2va_videos_and_audios_are_encoded_and_spoken) {
|
||||
CHECK(g.tokens == want);
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
FakeMultimodal mm;
|
||||
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.n_cenc, 2); /* both anchors canvas-resized ... */
|
||||
CHECK_EQ(mm.n_ienc, 0); /* ... none 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((int)g.refs[1].kind, (int)gen::VideoRefMedia::LAST_FRAME);
|
||||
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 */
|
||||
}
|
||||
|
||||
/* A named keyframe task must carry its frame: fl2va wants both ends. */
|
||||
TEST(a_keyframe_task_refuses_a_missing_frame) {
|
||||
FakeVideoGen gen;
|
||||
std::unique_ptr<Fixture> f(new Fixture(Fixture::Tweak{ [&](ServerOptions& o, Deps& d) {
|
||||
d.videos = &gen;
|
||||
o.video_model = true;
|
||||
gen.inf.encodes = 1;
|
||||
gen.inf.enc_ratio = 16;
|
||||
} }));
|
||||
/* fl2va with a first but no last -> names last_frame. */
|
||||
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" } };
|
||||
HttpResponse r = f->srv->router().dispatch(q);
|
||||
CHECK(r.status >= 400);
|
||||
CHECK(r.body.find("last_frame") != std::string::npos);
|
||||
}
|
||||
|
||||
/* Everything about the parts that cannot be right is refused naming the part (or `task` when
|
||||
* the media themselves are the question), before anything is queued. */
|
||||
TEST(ref2va_requests_are_refused_naming_the_part) {
|
||||
|
||||
Reference in new issue
Block a user