diff --git a/abi/rad_builder.h b/abi/rad_builder.h index fe09fc29..9ac5d26c 100644 --- a/abi/rad_builder.h +++ b/abi/rad_builder.h @@ -1253,7 +1253,10 @@ int rad_declare_audio_generator(RadBuilder* b, const RadAudioGenDecl* d); * encoder. * * `enc_kind` (RAD_ENC_*, rad_runtime.h) and `enc_slot`, the media's 1-based number within its kind, - * say where the latents go. The media's shape is the plugin's to accept: rad_arch_encode_geom + * say where the latents go. A latent-upscaled request's keyframes, pinned on the grid its steps + * run on, are encoded at W' x H' before the prompt pass and again at W x H, into the same slots, + * after the VUPSCALE pass and before the first evaluation at W x H; references, in their own grid, + * are encoded once. The media's shape is the plugin's to accept: rad_arch_encode_geom * (below) refuses what the encoders cannot take, and the step's packed sequence with the encoded * media in it is the plugin's rad_arch_video_pack. * diff --git a/core/gen/video.h b/core/gen/video.h index 96e65474..72e9f97b 100644 --- a/core/gen/video.h +++ b/core/gen/video.h @@ -98,6 +98,10 @@ struct VideoRefMedia { std::vector enc_samples; int64_t enc_n = 0; int32_t enc_channels = 0; + /* A keyframe of a latent-upscaled request again at the small size, prepared as at the full + * one: the steps before the upscale pin it on their own grid. Empty otherwise. */ + std::vector small_pixels; + int32_t small_h = 0, small_w = 0; }; struct VideoRequest { diff --git a/core/gen/video_driver.cpp b/core/gen/video_driver.cpp index 41f6020a..caa318ad 100644 --- a/core/gen/video_driver.cpp +++ b/core/gen/video_driver.cpp @@ -391,6 +391,7 @@ int VideoDriver::plan_refs(Job& j, const VideoRequest& r, int32_t frames, std::s }; j.ref_tab.clear(); j.enc_q.clear(); + j.enc_pre = 0; if (r.refs.empty()) return RAD_OK; if (!pack_ || !enc_geom_) return no("this model conditions on no reference media"); @@ -466,6 +467,7 @@ int VideoDriver::plan_refs(Job& j, const VideoRequest& r, int32_t frames, std::s } j.ref_tab[1] = (int32_t)keys.size() / 3; j.ref_tab.insert(j.ref_tab.end(), keys.begin(), keys.end()); + j.enc_pre = j.enc_q.size(); /* The prompt's vision pads: each picture's tokens, which ComfyUI modulates as video. */ const size_t at = j.ref_tab.size(); j.ref_tab.push_back(0); @@ -510,6 +512,33 @@ int VideoDriver::lay_out(const RadVideoGeom& g, const std::vector& tab, return RAD_OK; } +/* An upscaled request's keyframes, pinned on the grid its steps run on: encoded at the small size + * before the prompt, then at the full size into the same slots once the upscale is done. */ +int VideoDriver::plan_small_keys(Job& j, const VideoRequest& r, std::string* why) const { + const RadVideoGenDecl& d = decl_; + std::vector q; + int32_t eg[kEncGeomCap]; + for (const Job::EncItem& it : j.enc_q) { + if (it.kind != RAD_ENC_KEYFRAME) continue; + const VideoRefMedia& m = r.refs[(size_t)it.ref]; + const int64_t in[4] = { m.enc_frames, m.small_h, m.small_w, 3 }; + if (m.small_pixels.size() != (size_t)m.enc_frames * m.small_h * m.small_w * 3 || + d.enc_ratio < 1 || m.small_h % d.enc_ratio || m.small_w % d.enc_ratio || + m.small_h / d.enc_ratio != j.geom_small.lat_h || + m.small_w / d.enc_ratio != j.geom_small.lat_w || + enc_geom_(RAD_GEN_PASS_VENCODE, in, eg, kEncGeomCap) < 0) { + if (why) *why = "'" + m.name + "' has no picture at the latent upscale's small size " + "the model's encoder takes"; + return RAD_E_INVAL; + } + q.push_back({ it.kind, it.slot, it.ref, true }); + } + j.enc_pre = q.size(); + q.insert(q.end(), j.enc_q.begin(), j.enc_q.end()); + j.enc_q = std::move(q); + return RAD_OK; +} + /* The request's shape at `chunks` long: its geometry, rows and positions, its schedule, the * upscale's split and what its ticket says it makes. Run at submit, and again for a request whose * length the model predicts once its VCOND pass has written the prediction. */ @@ -544,14 +573,7 @@ int VideoDriver::shape_job(Job& j, const VideoRequest& r, int32_t chunks, int32_ RAD_TRY(plan_refs(j, r, s.frames, why)); j.pack = Pack{}; j.pack_small = Pack{}; - if (!j.ref_tab.empty()) { - /* A keyframe is pinned on the video's grid, which an upscale's small steps are not on. */ - if (r.upscale > 1.0 && j.ref_tab[1] > 0) - return no("a keyframe pins a frame of the video at its size, and latent_upscale " - "denoises a smaller one first: send the keyframe request without " - "latent_upscale"); - RAD_TRY(lay_out(j.geom, j.ref_tab, &j.pack, why)); - } + if (!j.ref_tab.empty()) RAD_TRY(lay_out(j.geom, j.ref_tab, &j.pack, why)); /* The schedule follows the upscale's split, which needs the small size first. */ const bool upscaling = r.upscale > 1.0; const bool own_refine = upscaling && d.n_refine_rungs > 0; @@ -610,6 +632,7 @@ int VideoDriver::shape_job(Job& j, const VideoRequest& r, int32_t chunks, int32_ if (positions_(RAD_VPOS_STEP, &j.geom_small, j.pos_small.data(), ns) != ns) return no("the model could not lay the small size out"); if (!j.ref_tab.empty()) RAD_TRY(lay_out(j.geom_small, j.ref_tab, &j.pack_small, why)); + if (!j.ref_tab.empty() && j.ref_tab[1] > 0) RAD_TRY(plan_small_keys(j, r, why)); j.split = evals - refine; j.factor = (float)(((double)r.width / sw + (double)r.height / sh) / 2.0); r.ticket->small_width = sw; @@ -935,18 +958,20 @@ const RadBatch* VideoDriver::next(int step) { for (auto& jp : jobs_) { Job& j = *jp; if (!j.admitted || j.failed || j.cond_done) continue; - if (j.enc_next < j.enc_q.size()) return build_encode(j, step); + if (j.enc_next < j.enc_pre) return build_encode(j, step); } /* 3. A prompt. */ for (auto& jp : jobs_) { Job& j = *jp; if (j.admitted && !j.failed && !j.cond_done) return build_cond(j, step); } - /* 4. The oldest lane's next step, or its upscale once its small steps are done. */ + /* 4. The oldest lane's next step, or its upscale once its small steps are done, and after it + * the keyframes again at the full size. */ for (auto& jp : jobs_) { Job& j = *jp; if (!j.admitted || j.failed || !j.cond_done || j.step >= j.sched.steps) continue; if (small_now(j) && j.step == j.split) return build_upscale(j, step); + if (!small_now(j) && j.enc_next < j.enc_q.size()) return build_encode(j, step); return build_step(j, step); } return nullptr; @@ -1189,9 +1214,12 @@ const RadBatch* VideoDriver::build_encode(Job& j, int step) { const Job::EncItem& it = j.enc_q[j.enc_next]; const VideoRefMedia& m = j.req->refs[(size_t)it.ref]; const bool audio = it.kind == RAD_ENC_VAUDIO || it.kind == RAD_ENC_AUDIO; + /* A keyframe at an upscale's small size is the media's second picture. */ + const std::vector& pix = it.small ? m.small_pixels : m.enc_pixels; + const int32_t eh = it.small ? m.small_h : m.enc_h, ew = it.small ? m.small_w : m.enc_w; int32_t* gd = hptr(R_ENCG); - const int64_t in[4] = { audio ? m.enc_n : m.enc_frames, audio ? 0 : m.enc_h, - audio ? 0 : m.enc_w, audio ? m.enc_channels : 3 }; + const int64_t in[4] = { audio ? m.enc_n : m.enc_frames, audio ? 0 : eh, audio ? 0 : ew, + audio ? m.enc_channels : 3 }; const int64_t ng = enc_geom_(audio ? RAD_GEN_PASS_AENCODE : RAD_GEN_PASS_VENCODE, in, gd, kEncGeomCap); int64_t stage_frames = 0, stage_samples = 0; @@ -1208,14 +1236,14 @@ const RadBatch* VideoDriver::build_encode(Job& j, int step) { std::memcpy(sm + c * stage_samples, m.enc_samples.data() + c * n, (size_t)n * 4); reg_[R_ENCSM].used = stage_samples * ch * 4; } else { - const int64_t nf = m.enc_frames, area = (int64_t)m.enc_h * m.enc_w * 3; + const int64_t nf = m.enc_frames, area = (int64_t)eh * ew * 3; stage_frames = ng > 23 ? gd[23] : 0; if (stage_frames < nf || stage_frames * area * 4 > reg_[R_ENCPX].bytes) { fail_locked(j, RAD_E_STATE, "'" + m.name + "' does not fit an encode pass"); return nullptr; } float* px = hptr(R_ENCPX); - std::memcpy(px, m.enc_pixels.data(), (size_t)(nf * area) * 4); + std::memcpy(px, pix.data(), (size_t)(nf * area) * 4); for (int64_t f = nf; f < stage_frames; ++f) std::memcpy(px + f * area, px + (nf - 1) * area, (size_t)area * 4); reg_[R_ENCPX].used = stage_frames * area * 4; @@ -1238,8 +1266,8 @@ const RadBatch* VideoDriver::build_encode(Job& j, int step) { } else { V.enc_pixels = dptr(ri, R_ENCPX); V.enc_frames = (int32_t)stage_frames; - V.enc_height = m.enc_h; - V.enc_width = m.enc_w; + V.enc_height = eh; + V.enc_width = ew; } } const int32_t kind = audio ? RAD_GEN_PASS_AENCODE : RAD_GEN_PASS_VENCODE; diff --git a/core/gen/video_driver.h b/core/gen/video_driver.h index cc486689..9a3f876a 100644 --- a/core/gen/video_driver.h +++ b/core/gen/video_driver.h @@ -16,9 +16,10 @@ * 2. VENCODE or AENCODE of an admitted request's reference media, one per reference, so the * video and audio VAE latents are in the lane's slots before its prompt runs. * 3. VCOND of an admitted request, so its steps can follow the current request's without a gap. - * 4. VSTEP of the oldest lane with steps left, or its VUPSCALE once its small steps are done. - * Requests are denoised one after another, not in turns: a pass is one request's whole - * sequence, so taking turns would only delay the first. + * 4. VSTEP of the oldest lane with steps left, or its VUPSCALE once its small steps are done, + * then its keyframes' VENCODEs at the full size (an upscaled request's keyframes are encoded + * at the small size in 2). Requests are denoised one after another, not in turns: a pass is + * one request's whole sequence, so taking turns would only delay the first. */ #pragma once #include "video.h" @@ -86,12 +87,15 @@ private: std::vector pos; /* [3, rows] */ /* References and keyframes: their table (RadVideoGenBatch::ref_tab), the step's packed * sequence at the full size and at an upscale's small one (rows 0 without references), - * and the encode passes, one a media in order, `enc_next` the next to run. */ + * and the encode passes, one a media in order, `enc_next` the next to run. The first + * `enc_pre` run before the prompt; the rest, an upscaled request's keyframes at the full + * size, run into the same slots once the upscale is done. */ std::vector ref_tab; Pack pack, pack_small; - struct EncItem { int32_t kind, slot, ref; }; /* RAD_ENC_*, its number, its req->refs */ + /* RAD_ENC_*, its number, its req->refs, and whether it is the media's small_pixels. */ + struct EncItem { int32_t kind, slot, ref; bool small = false; }; std::vector enc_q; - size_t enc_next = 0; + size_t enc_next = 0, enc_pre = 0; /* Latent upscaling: the evaluations at the small size (0: none), its geometry, rows and * positions, the factor the network is told, and whether the upscale has run. */ int32_t split = 0; @@ -143,6 +147,7 @@ private: bool small_now(const Job& j) const { return j.split > 0 && !j.upscaled; } int shape_job(Job& j, const VideoRequest& r, int32_t chunks, int32_t steps, std::string* why); int plan_refs(Job& j, const VideoRequest& r, int32_t frames, std::string* why) const; + int plan_small_keys(Job& j, const VideoRequest& r, std::string* why) const; int lay_out(const RadVideoGeom& g, const std::vector& tab, Pack* out, std::string* why) const; int32_t predicted_chunks(double log_seconds, std::string* note) const; diff --git a/core/server/videos.cpp b/core/server/videos.cpp index 76e090c6..53ac4396 100644 --- a/core/server/videos.cpp +++ b/core/server/videos.cpp @@ -38,7 +38,8 @@ * first steps run at `size` divided by it and the latent is enlarged to `size` for the last * `upscale_refine_steps` (the model's default when absent; abi/rad_builder.h, RadVideoGenDecl). * The video object of an upscaled video says how it was made: the factor, the refine steps and the - * small size (`upscale_from`). + * small size (`upscale_from`). Keyframes ride it: each is prepared at both sizes and pinned at the + * one the steps run at. * * The prompt is the model's template with `{prompt}` filled; the template is tokenised with its * special tokens and the prompt without, so a caller cannot write control tokens into it. @@ -742,6 +743,23 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) { &canvas[i], &why) < 0) return bad(m.name.c_str(), "'" + m.name + "' could not be decoded: " + why); pixels(m, { canvas[i] }); + /* A latent upscale's first steps pin it at their own size, prepared the same way. */ + if (r->upscale > 1.0) { + RadVideoGenDecl geo{}; /* video_small_size reads the size multiple only */ + geo.size_multiple = info.size_multiple; + int32_t sw = 0, sh = 0; + gen::video_small_size(geo, r->width, r->height, r->upscale, &sw, &sh); + mm::Frame small; + if (deps_.mm->encoder_image_canvas((const uint8_t*)m.data.data(), m.data.size(), + sw, sh, + m.kind == gen::VideoRefMedia::LAST_FRAME || + first_center, + &small, &why) < 0) + return bad(m.name.c_str(), "'" + m.name + "' could not be decoded: " + why); + m.small_h = small.h; + m.small_w = small.w; + m.small_pixels.assign(small.rgb.begin(), small.rgb.end()); + } } } diff --git a/docs/GUIDE.md b/docs/GUIDE.md index a0b97a18..058b7b39 100644 --- a/docs/GUIDE.md +++ b/docs/GUIDE.md @@ -3103,7 +3103,8 @@ workflow, and is otherwise read off the parts: - **Keyframes**: a `first_frame` (`i2va`), a `last_frame` (`l2va`) or both (`fl2va`), each an image pinned at that end of the video. The first frame is stretched to the video's size, or, with `"first_frame_mode": "center"`, scaled to cover it and cropped at the centre; the last frame is - always cropped. A keyframe request takes no `latent_upscale`: the pins are on the video's own grid. + always cropped. With `latent_upscale` each keyframe is prepared at both sizes and pinned on the + grid the steps run on (below). - **Reference media** (`ref2va`): `ref_image_1` .. `ref_image_9`, `ref_video_1` .. `ref_video_3`, each video with an optional soundtrack `ref_video_audio_N` of its own number, and `ref_audio_1` .. `ref_audio_3`, numbered from 1 without gaps. Pictures and video frames are fitted into 256 x 256 @@ -3142,7 +3143,9 @@ upscaler, re-noised, and the last **`upscale_refine_steps`** run at the full siz and 12 of the base model's 49 by default). The steps at the small size cost a fraction of a full-size step's, so most of the time goes to the refinement. The video object of an upscaled video carries `latent_upscale`, the `upscale_refine_steps` it ran and `upscale_from`, the small -size. Reference media ride it unchanged; keyframes do not (above). +size. Reference media ride it unchanged. Keyframes ride it too: each is encoded at the small size +before the prompt and again at the full size after the upscale, into the same slot, so every step +pins it on its own grid. ```sh curl -s http://localhost:8000/v1/videos -H 'Content-Type: application/json' -d '{ diff --git a/integrations/comfyui/ComfyUI-Radiance-MiniMax/README.md b/integrations/comfyui/ComfyUI-Radiance-MiniMax/README.md index c7938ad7..3a0b477d 100644 --- a/integrations/comfyui/ComfyUI-Radiance-MiniMax/README.md +++ b/integrations/comfyui/ComfyUI-Radiance-MiniMax/README.md @@ -54,8 +54,7 @@ and at least 256, an aspect of at most 4:1 and an area of at most 1344x768, so 7 console. Every task takes every size. `latent_upscale` (advanced) runs the early steps at half or a quarter of the size and the rest -at full size, with a container converted with the latent upscaler. The keyframe tasks do not take -it. +at full size, with a container converted with the latent upscaler. Every task takes it. ## Outputs diff --git a/integrations/comfyui/ComfyUI-Radiance-MiniMax/radiance_node.py b/integrations/comfyui/ComfyUI-Radiance-MiniMax/radiance_node.py index 7448fab2..82dd4901 100644 --- a/integrations/comfyui/ComfyUI-Radiance-MiniMax/radiance_node.py +++ b/integrations/comfyui/ComfyUI-Radiance-MiniMax/radiance_node.py @@ -347,8 +347,7 @@ class RadianceMiniMax(io.ComfyNode): io.Combo.Input("latent_upscale", options=["off", "2", "4"], default="off", advanced=True, tooltip="run the early steps at 1/2 or 1/4 of the size, with a " - "container that has the latent upscaler; not with " - "keyframes"), + "container that has the latent upscaler"), *images, *videos, *tracks, *audios, io.Int.Input("timeout", default=1800, min=30, max=7200, step=30, advanced=True, tooltip="seconds to wait for the job"), diff --git a/tests/video_gen_test.cpp b/tests/video_gen_test.cpp index bc49c4e8..7464572f 100644 --- a/tests/video_gen_test.cpp +++ b/tests/video_gen_test.cpp @@ -1080,8 +1080,7 @@ TEST(a_keyframe_pair_is_encoded_and_pins_both_ends) { CHECK(stepped); } -/* References ride a latent upscale, laid out again at the small size for the steps before it; a - * keyframe, pinned on the video's own grid, does not. */ +/* References ride a latent upscale, laid out again at the small size for the steps before it. */ TEST(references_are_laid_out_again_at_an_upscales_small_size) { VideoFixture f; f.build(); @@ -1120,6 +1119,53 @@ TEST(references_are_laid_out_again_at_an_upscales_small_size) { CHECK_OK(f.drv.done()); } CHECK_EQ(kinds, std::string("ECSSUSAD")); +} + +/* A keyframe rides a latent upscale pinned on the grid the steps run on: encoded at the small size + * before the prompt, and again at the full size, into the same slot, between the upscale and the + * first full-size step. Without its small picture the request is refused. */ +TEST(a_keyframe_is_encoded_again_at_the_full_size_after_an_upscale) { + VideoFixture f; + f.build(); + f.prog.video_gen.upscale_max = 4.0; + f.prog.video_gen.upscale_refine = 1; + with_tower(f, 16); + CHECK_OK(f.start_refs()); + std::string why; + auto r = f.req(1, 4); + r->width = 64; + r->height = 64; + r->upscale = 2.0; + gen::VideoRefMedia kf = picture(gen::VideoRefMedia::FIRST_FRAME, "first_frame", 1, 64, 64); + kf.small_w = 32; + kf.small_h = 32; + kf.small_pixels.assign((size_t)32 * 32 * 3, 2.0f); + r->refs.push_back(kf); + const int64_t T = (int64_t)r->tokens.size(); + CHECK_OK(f.drv.submit(std::move(r), &why)); + + /* The prompt, a keyframe's 2 rows and 60 soundtrack rows, then a video of 2 rows small, 8 full. */ + std::string kinds; + int steps = 0, encodes = 0; + for (int i = 0; i < 20; ++i) { + const RadBatch* b = f.drv.next(i); + if (!b) break; + const RadVideoGenBatch* V = b->vgen_b; + kinds += b->gen == RAD_GEN_PASS_VENCODE ? 'E' : kind_of(b->gen); + if (b->gen == RAD_GEN_PASS_VENCODE) { + const int32_t side = encodes++ == 0 ? 32 : 64; + CHECK_EQ(V->enc_kind, (int32_t)RAD_ENC_KEYFRAME); + CHECK_EQ(V->enc_slot, 1); + CHECK_EQ(V->enc_height, side); + CHECK_EQ(V->enc_width, side); + } else if (b->gen == RAD_GEN_PASS_VSTEP) { + CHECK_EQ(b->n_tok, T + 2 + 60 + (steps++ < 2 ? 2 : 8)); + CHECK_EQ(V->ref_tab[1], 1); + } + f.answer(10 * i); + CHECK_OK(f.drv.done()); + } + CHECK_EQ(kinds, std::string("ECSSUESAD")); VideoFixture g; g.build(); @@ -1132,7 +1178,7 @@ TEST(references_are_laid_out_again_at_an_upscales_small_size) { r->upscale = 2.0; r->refs.push_back(picture(gen::VideoRefMedia::FIRST_FRAME, "first_frame", 1, 64, 64)); CHECK_EQ(g.drv.submit(std::move(r), &why), (int)RAD_E_INVAL); - CHECK(has(why, "latent_upscale")); + CHECK(has(why, "small size")); } /* A reference video with its soundtrack and a standalone soundtrack: one encode pass each, the diff --git a/tests/videos_test.cpp b/tests/videos_test.cpp index 107a4386..4420553a 100644 --- a/tests/videos_test.cpp +++ b/tests/videos_test.cpp @@ -680,6 +680,49 @@ TEST(fl2va_keyframes_feed_the_tower_and_pin_the_output_ends) { CHECK(g.tokens == want); } +/* With a latent upscale each keyframe is prepared twice, the same way: at the canvas and at the + * small size the first steps pin it at. The tower still sees the canvas frame only. */ +TEST(fl2va_keyframes_are_prepared_at_an_upscales_small_size_too) { + FakeMultimodal mm; + mm.expand = { 900, 901 }; + mm.enc_frames = 5; mm.enc_side = 16; mm.enc_n = 850; + FakeVideoGen gen; + std::unique_ptr f(new Fixture(Fixture::Tweak{ [&](ServerOptions& o, Deps& d) { + d.videos = &gen; + d.mm = &mm; + o.video_model = true; + gen.inf.enc_ratio = 16; + gen.inf.max_enc_side = 512; + gen.inf.upscale_max = 4.0; + } })); + HttpRequest q; + q.method = "POST"; + q.path = "/v1/videos"; + q.form = { FormPart{ "prompt", "", "", "fox" }, + FormPart{ "size", "", "", "512x256" }, + FormPart{ "task", "", "", "fl2va" }, + FormPart{ "latent_upscale", "", "", "2" }, + 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_pframe, 2); + REQUIRE_EQ(mm.cenc_covers.size(), (size_t)4); + CHECK(!mm.cenc_covers[0]); /* first frame: stretched at both sizes */ + CHECK(!mm.cenc_covers[1]); + CHECK(mm.cenc_covers[2]); /* last frame: centre-cropped at both sizes */ + CHECK(mm.cenc_covers[3]); + const gen::VideoRequest& g = gen.last(); + REQUIRE_EQ(g.refs.size(), (size_t)2); + for (const gen::VideoRefMedia& m : g.refs) { + CHECK_EQ(m.enc_w, 512); + CHECK_EQ(m.enc_h, 256); + CHECK_EQ(m.small_w, 256); + CHECK_EQ(m.small_h, 128); + CHECK_EQ(m.small_pixels.size(), (size_t)256 * 128 * 3); + } +} + /* A last frame alone is : the number counts the pictures present, as ComfyUI's does. * fl2va (both ends) keeps first = 1, last = 2. */ TEST(l2va_presents_its_sole_frame_as_picture_1) {