From d230b07e43543f952562dc091e228bcdd4c8ab3f Mon Sep 17 00:00:00 2001 From: unico123 <77184453+unico123@users.noreply.github.com> Date: Fri, 27 Nov 2026 13:15:40 +0800 Subject: [PATCH 1/6] docs(dgl): MiniMax-H3 AV continuation example (two-part seamless chain) --- .../minimax_h3/minimax-h3-av-continuation.dgl | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl diff --git a/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl b/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl new file mode 100644 index 0000000000..a474643bac --- /dev/null +++ b/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl @@ -0,0 +1,137 @@ +# version: 0.0.0 +# docs/models/minimax_h3.md — AV continuation: chain segment B onto segment A's +# tail latent (video head and audio head pinned as never-denoised), seamless +# multi-part video without pixel-frame anchoring drift. + +var { + model_path = "../models/diffusion_models/minimax_h3_fl2va-Q4_K_M.gguf" + text_path = "../models/text_encoders/qwen3vl_32b_minimax_h3-Q4_K_M.gguf" + vae_path = "../models/vae/minimax_h3_video_vae_fp16.safetensors" + audio_vae_path = "../models/vae/minimax_h3_audio_vae_fp32.safetensors" + + output_a_path = "continuation-part-a.avi" + output_b_path = "continuation-part-b.avi" + latent_a_path = "continuation-part-a.npy" + + prompt_a = "A cute American Shorthair silver tabby kitten surfs on a tropical ocean wave, cinematic tracking shot, bright sunlight. Upbeat tropical surf-rock background music with cheerful drums and guitar." + prompt_b = "The same kitten rides the wave toward the beach at sunset, spray in golden light, smooth motion. The surf-rock music continues seamlessly." + + seed = 42 + rng_type = "cpu" + width = 864 + height = 480 + frames = 56 + fps = 24 + + steps = 20 + method = "euler" + scheduler = "discrete" + audio_scale = 4 + + # latent frames carried from segment A's tail; clamped to the previous + # segment's length, and the same count pins audio latent frames + pin_frames = 4 +} + +fn pack_av(video, audio, audio_scale) { + shape = tensor::shape(video) + count = tensor::numel(audio) + tail_channels = math::ceil_div(count, shape[0] * shape[1] * shape[2]) + tail_shape = [shape[0], shape[1], shape[2], tail_channels, 1] + flat = tensor::reshape(tensor::mul(audio, audio_scale), [count]) + padded = tensor::pad(flat, [0, math::product(tail_shape) - count]) + return tensor::concat([video, tensor::reshape(padded, tail_shape)], axis = 3) +} + +fn unpack_audio(packed, channels, audio_shape, audio_scale) { + tail = packed[:, :, :, channels:] + flat = tensor::reshape(tail, [tensor::numel(tail)]) + return tensor::div(tensor::reshape(flat[:math::product(audio_shape)], audio_shape), audio_scale) +} + +# Fold an audio-space pattern [T, 2, 32, 1] into the packed latent's tail +# channels exactly the way pack_av folds audio content, so mask zeros land on +# the same flat positions as the pinned audio frames. +fn audio_pattern_to_tail(audio_pattern, video_shape_tensor) { + count = tensor::numel(audio_pattern) + shape = tensor::shape(video_shape_tensor) + tail_channels = math::ceil_div(count, shape[0] * shape[1] * shape[2]) + tail_shape = [shape[0], shape[1], shape[2], tail_channels, 1] + flat = tensor::reshape(audio_pattern, [count]) + padded = tensor::pad(flat, [0, math::product(tail_shape) - count], value = 1) + return tensor::reshape(padded, tail_shape) +} + +fn main() { + model = load_dit(model_path, family = "minimax_h3", params_backend = "cpu") + text = load_text_encoder(text_path, family = "minimax_h3", params_backend = "cpu") + vae = load_vae(vae_path, family = "minimax_h3", params_backend = "cpu") + audio_vae = load_audio_vae(audio_vae_path, family = "minimax_h3", params_backend = "cpu") + + video_base = tensor::zeros(latent_shape(vae, width = width, height = height, frames = frames)) + audio_zeros = tensor::zeros(audio_latent_shape(audio_vae, frames = frames, fps = fps)) + c_video = tensor::shape(video_base)[3] + t_video = tensor::shape(video_base)[2] + + # ---------- segment A ---------- + condition_a = encode_prompt(text, prompt_a, width = width, height = height) + base_a = pack_av(video_base, audio_zeros, audio_scale = audio_scale) + predict_a = predictor(model, condition_a, audio_length = tensor::shape(audio_zeros)[0]) + sigmas_a = make_sigmas(predict_a, latent = base_a, steps = steps, scheduler = scheduler) + rng = make_rng(rng_type, seed = seed) + start_a = add_noise(predict_a, base_a, tensor::randn_like(base_a, rng = rng), sigma = sigmas_a[0]) + latent_a = sample(start_a, sigmas_a, predict = predict_a, method = method, rng = rng) + + # save after sampling completes (the save point is the final latent, so a + # later refinement pass would naturally run before this line) + tensor::save(latent_a_path, latent_a) + + frames_a = vae_decode(vae, dit_to_vae_latents(vae, latent_a[:, :, :t_video, :c_video])) + audio_a = audio_vae_decode(audio_vae, unpack_audio(latent_a, channels = c_video, audio_shape = tensor::shape(audio_zeros), audio_scale = audio_scale)) + video::save(output_a_path, {frames = frames_a, fps_num = fps, fps_den = 1}, audio = audio_a) + + # ---------- segment B: continuation ---------- + packed_prev = tensor::load(latent_a_path) + video_prev = packed_prev[:, :, :, :c_video] + # audio latent is t-major [T, 2, 32, 1]: the tail slice carries whole + # streams, so no flat-offset math is needed even for unequal segments + audio_prev = unpack_audio(packed_prev, channels = c_video, audio_shape = tensor::shape(audio_zeros), audio_scale = audio_scale) + t_audio = tensor::shape(audio_zeros)[0] + # negative-endpoint slices clip to bounds, so the pinned tail length is + # min(pin_frames, previous segment length, current segment length) + video_tail_raw = video_prev[:, :, -pin_frames:, :] + video_tail = video_tail_raw[:, :, -t_video:, :] + audio_tail_raw = audio_prev[-pin_frames:, :, :, :] + audio_tail = audio_tail_raw[-t_audio:, :, :, :] + h = tensor::shape(video_tail)[2] + n = tensor::shape(audio_tail)[0] + video_base_b = tensor::replace_slice(tensor::zeros(tensor::shape(video_base)), video_tail, axis = 2, start = 0) + audio_b = tensor::replace_slice(tensor::zeros(tensor::shape(audio_zeros)), audio_tail, axis = 0, start = 0) + base_b = pack_av(video_base_b, audio_b, audio_scale = audio_scale) + + # full-width denoise mask over the packed AV latent; zeros pin the carried + # heads exactly (per-step restore: denoised * mask + base * (1 - mask)). + # Audio zeros are folded with the same tail-channel layout as the content. + mask_video = tensor::concat([ + tensor::full([tensor::shape(video_base)[0], tensor::shape(video_base)[1], h, c_video, 1], 0), + tensor::full([tensor::shape(video_base)[0], tensor::shape(video_base)[1], t_video - h, c_video, 1], 1) + ], axis = 2) + mask_audio_pattern = tensor::concat([ + tensor::full([n, tensor::shape(audio_zeros)[1], tensor::shape(audio_zeros)[2], tensor::shape(audio_zeros)[3]], 0), + tensor::full([t_audio - n, tensor::shape(audio_zeros)[1], tensor::shape(audio_zeros)[2], tensor::shape(audio_zeros)[3]], 1) + ], axis = 0) + mask_b = tensor::concat([mask_video, audio_pattern_to_tail(mask_audio_pattern, video_base)], axis = 3) + + condition_b = encode_prompt(text, prompt_b, width = width, height = height) + predict_b = predictor(model, condition_b, audio_length = tensor::shape(audio_zeros)[0]) + sigmas_b = make_sigmas(predict_b, latent = base_b, steps = steps, scheduler = scheduler) + rng_b = make_rng(rng_type, seed = seed + 1) + start_b = add_noise(predict_b, base_b, tensor::randn_like(base_b, rng = rng_b), sigma = sigmas_b[0]) + latent_b = sample(start_b, sigmas_b, predict = predict_b, method = method, rng = rng_b, + base = base_b, denoise_mask = mask_b, mask_input = true) + + frames_b = vae_decode(vae, dit_to_vae_latents(vae, latent_b[:, :, :t_video, :c_video])) + audio_b_out = audio_vae_decode(audio_vae, unpack_audio(latent_b, channels = c_video, audio_shape = tensor::shape(audio_zeros), audio_scale = audio_scale)) + video::save(output_b_path, {frames = frames_b, fps_num = fps, fps_den = 1}, audio = audio_b_out) + return {video = {frames = frames_b, fps_num = fps, fps_den = 1}, latent = latent_b, audio = audio_b_out} +} From 50fdca54a99867de4d71ab554a76099a099ab309 Mon Sep 17 00:00:00 2001 From: unico123 <77184453+unico123@users.noreply.github.com> Date: Fri, 27 Nov 2026 13:15:43 +0800 Subject: [PATCH 2/6] docs(dgl): index the MiniMax-H3 AV continuation example From 23cf485dd1e0439b38268f7afcc66733ac913829 Mon Sep 17 00:00:00 2001 From: unico123 <77184453+unico123@users.noreply.github.com> Date: Sat, 28 Nov 2026 21:49:30 +0800 Subject: [PATCH 3/6] docs(dgl): tile H3 AV continuation VAE decode; route VAEs to iGPU on 8G cards - Monolithic video VAE decode exhausts 8 GiB cards (Vulkan device-lost at the VAE graph); 256x256 tiling fixes it. - On the same test machine (Navi10 dGPU) the VAE graphs device-lost on the default device even with tiling; routing them to the iGPU unified pool resolves it (noted in a script comment). Verified end-to-end at 640x352x24f, 20 steps, pin_frames=2: both segments render with audio, pinned-head continuity confirmed numerically (MAE 5.2 vs 55-60 baseline). Co-Authored-By: Claude Code --- .../minimax_h3/minimax-h3-av-continuation.dgl | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl b/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl index a474643bac..8b53e73060 100644 --- a/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl +++ b/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl @@ -63,10 +63,12 @@ fn audio_pattern_to_tail(audio_pattern, video_shape_tensor) { } fn main() { - model = load_dit(model_path, family = "minimax_h3", params_backend = "cpu") - text = load_text_encoder(text_path, family = "minimax_h3", params_backend = "cpu") - vae = load_vae(vae_path, family = "minimax_h3", params_backend = "cpu") - audio_vae = load_audio_vae(audio_vae_path, family = "minimax_h3", params_backend = "cpu") + model = load_dit(model_path, family = "minimax_h3", params_backend = "cpu", backend = "vulkan1") + text = load_text_encoder(text_path, family = "minimax_h3", params_backend = "cpu", backend = "vulkan1") + # note: on one test machine (Navi10 dGPU, 8 GiB) the VAE graphs device-lost + # on the default device; routing them to the iGPU's larger unified pool fixed it + vae = load_vae(vae_path, family = "minimax_h3", params_backend = "cpu", backend = "vulkan1") + audio_vae = load_audio_vae(audio_vae_path, family = "minimax_h3", params_backend = "cpu", backend = "vulkan1") video_base = tensor::zeros(latent_shape(vae, width = width, height = height, frames = frames)) audio_zeros = tensor::zeros(audio_latent_shape(audio_vae, frames = frames, fps = fps)) @@ -86,7 +88,8 @@ fn main() { # later refinement pass would naturally run before this line) tensor::save(latent_a_path, latent_a) - frames_a = vae_decode(vae, dit_to_vae_latents(vae, latent_a[:, :, :t_video, :c_video])) + frames_a = vae_decode(vae, dit_to_vae_latents(vae, latent_a[:, :, :t_video, :c_video]), + tiling = {enabled = true, tile_size_w = 256, tile_size_h = 256}) audio_a = audio_vae_decode(audio_vae, unpack_audio(latent_a, channels = c_video, audio_shape = tensor::shape(audio_zeros), audio_scale = audio_scale)) video::save(output_a_path, {frames = frames_a, fps_num = fps, fps_den = 1}, audio = audio_a) @@ -130,7 +133,8 @@ fn main() { latent_b = sample(start_b, sigmas_b, predict = predict_b, method = method, rng = rng_b, base = base_b, denoise_mask = mask_b, mask_input = true) - frames_b = vae_decode(vae, dit_to_vae_latents(vae, latent_b[:, :, :t_video, :c_video])) + frames_b = vae_decode(vae, dit_to_vae_latents(vae, latent_b[:, :, :t_video, :c_video]), + tiling = {enabled = true, tile_size_w = 256, tile_size_h = 256}) audio_b_out = audio_vae_decode(audio_vae, unpack_audio(latent_b, channels = c_video, audio_shape = tensor::shape(audio_zeros), audio_scale = audio_scale)) video::save(output_b_path, {frames = frames_b, fps_num = fps, fps_den = 1}, audio = audio_b_out) return {video = {frames = frames_b, fps_num = fps, fps_den = 1}, latent = latent_b, audio = audio_b_out} From 90d2ed0c4c77422d74fa14c51b7a0162374f06cd Mon Sep 17 00:00:00 2001 From: unico123 <77184453+unico123@users.noreply.github.com> Date: Sat, 28 Nov 2026 22:04:28 +0800 Subject: [PATCH 4/6] docs(dgl): derive both pinned overlaps from one time interval Address review: the video and audio pinned latent counts now both derive from a single `overlap_frames` pixel-frame interval (5 video latents span 17 frames; audio VAE emits 40 latents/s), the overlap is capped so segment B always keeps at least half of its latents to continue, overlap 0 degrades cleanly to a plain continuation (positive-index slices, no negative-endpoint zero-count footgun), and the return value reports the exact join point: trim `overlap_frames` from both decoded streams before concatenating. Co-Authored-By: Claude Code --- .../minimax_h3/minimax-h3-av-continuation.dgl | 56 ++++++++++++++++--- 1 file changed, 48 insertions(+), 8 deletions(-) diff --git a/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl b/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl index 8b53e73060..6397a4210d 100644 --- a/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl +++ b/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl @@ -2,6 +2,14 @@ # docs/models/minimax_h3.md — AV continuation: chain segment B onto segment A's # tail latent (video head and audio head pinned as never-denoised), seamless # multi-part video without pixel-frame anchoring drift. +# +# The overlap is ONE time interval, `overlap_frames` pixel frames of A's tail. +# Video and audio latent counts are both derived from it, so the streams stay +# aligned for any `frames`. To join A and B: decode B, then drop its first +# `overlap_frames` pixel frames of video and the matching overlap duration of +# audio before concatenating (see the join note in main() below — the pinned +# latents land at different positions within Sol-H3's five-latent decoder +# blocks than they occupied in A, so trimming is what makes the seam clean). var { model_path = "../models/diffusion_models/minimax_h3_fl2va-Q4_K_M.gguf" @@ -28,9 +36,13 @@ var { scheduler = "discrete" audio_scale = 4 - # latent frames carried from segment A's tail; clamped to the previous - # segment's length, and the same count pins audio latent frames - pin_frames = 4 + # Pixel frames of segment A's tail carried into segment B's head — the + # single time interval both pinned latent counts derive from. 0 means no + # pinning: B degrades to a plain continuation. main() caps the derived + # counts so B always keeps at least half of its latents to generate. + # Default 12 frames at 24 fps = 0.5 s = 4 video latents (17-frame blocks, + # 5 latents each) = 20 audio latents (40 per second). + overlap_frames = 12 } fn pack_av(video, audio, audio_scale) { @@ -94,17 +106,34 @@ fn main() { video::save(output_a_path, {frames = frames_a, fps_num = fps, fps_den = 1}, audio = audio_a) # ---------- segment B: continuation ---------- + # Sol-H3 timing: video latents come in five-latent/17-frame blocks after + # the first (video_frames_to_latent_frames: frames <= 5 -> 2 latents, else + # ((frames - 5) / 17) * 5 + 2; decoded_frames(t) = 4t - 3*ceil(t/5)), and + # the audio VAE emits 40 latents per second. Both pinned counts derive + # from the SAME `overlap_frames` pixel-frame interval, so the video and + # audio overlaps always cover the same duration for any `frames`. packed_prev = tensor::load(latent_a_path) video_prev = packed_prev[:, :, :, :c_video] # audio latent is t-major [T, 2, 32, 1]: the tail slice carries whole # streams, so no flat-offset math is needed even for unequal segments audio_prev = unpack_audio(packed_prev, channels = c_video, audio_shape = tensor::shape(audio_zeros), audio_scale = audio_scale) t_audio = tensor::shape(audio_zeros)[0] - # negative-endpoint slices clip to bounds, so the pinned tail length is - # min(pin_frames, previous segment length, current segment length) - video_tail_raw = video_prev[:, :, -pin_frames:, :] + t_prev = tensor::shape(video_prev)[2] + ta_prev = tensor::shape(audio_prev)[0] + + h_req = math::ceil_div(overlap_frames * 5, 17) # video latents covering the interval + n_req = math::ceil_div(overlap_frames * 40, fps) # audio latents covering the interval + # Cap the overlap so B keeps room to continue: never carry more than half + # of B's latents on either stream (nor more than A's tail can give). + # overlap_frames = 0 leaves h_req = n_req = 0: the slices below go empty, + # the mask becomes all-ones, and B is a plain continuation — handled + # without special-casing. Positive-index slices are load-bearing here: a + # negative-endpoint slice with a zero count would select the WHOLE tail. + h_cap = math::min(math::min(h_req, math::ceil_div(t_video, 2)), t_prev) + n_cap = math::min(math::min(n_req, math::ceil_div(t_audio, 2)), ta_prev) + video_tail_raw = video_prev[:, :, t_prev - h_cap:, :] video_tail = video_tail_raw[:, :, -t_video:, :] - audio_tail_raw = audio_prev[-pin_frames:, :, :, :] + audio_tail_raw = audio_prev[ta_prev - n_cap:, :, :, :] audio_tail = audio_tail_raw[-t_audio:, :, :, :] h = tensor::shape(video_tail)[2] n = tensor::shape(audio_tail)[0] @@ -137,5 +166,16 @@ fn main() { tiling = {enabled = true, tile_size_w = 256, tile_size_h = 256}) audio_b_out = audio_vae_decode(audio_vae, unpack_audio(latent_b, channels = c_video, audio_shape = tensor::shape(audio_zeros), audio_scale = audio_scale)) video::save(output_b_path, {frames = frames_b, fps_num = fps, fps_den = 1}, audio = audio_b_out) - return {video = {frames = frames_b, fps_num = fps, fps_den = 1}, latent = latent_b, audio = audio_b_out} + + # Join point: decoded_frames(t) = 4t - 3*ceil(t/5) for Sol-H3, so the h + # pinned head latents reconstruct covered_video frames of A's tail (>= + # overlap_frames). Trim exactly `overlap_frames` from BOTH streams — + # B's first overlap_frames pixel frames of video and the matching + # overlap_frames/fps seconds of audio — then concatenate A. Both trims + # come from the same interval, so the streams stay in sync for any + # `frames`. covered_* are the exact reconstructed counts, for checking. + covered_video = (4 * t_video - 3 * math::ceil_div(t_video, 5)) - (4 * (t_video - h) - 3 * math::ceil_div(t_video - h, 5)) + covered_audio = math::ceil_div(n * fps, 40) + return {video = {frames = frames_b, fps_num = fps, fps_den = 1}, latent = latent_b, audio = audio_b_out, + join = {trim_frames = overlap_frames, covered_video_frames = covered_video, covered_audio_frames = covered_audio}} } From 682a74230f2572ce9d99665b6fb9b906710ec304 Mon Sep 17 00:00:00 2001 From: unico123 <77184453+unico123@users.noreply.github.com> Date: Sat, 28 Nov 2026 22:04:30 +0800 Subject: [PATCH 5/6] docs(dgl): index the MiniMax-H3 AV continuation example Co-Authored-By: Claude Code --- docs/dgl/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/dgl/README.md b/docs/dgl/README.md index df8958cc69..65b31332fd 100644 --- a/docs/dgl/README.md +++ b/docs/dgl/README.md @@ -91,7 +91,7 @@ and [NumPy tensor files](../dgl.md#tensor-files). | [LingBot-Video](../models/lingbot_video.md) | [Dens 1.3B text to video](lingbot_video/lingbot-video-dens-1-3b-txt2video.dgl) | | [LTX-2.3](../models/ltx2.md) | [2.3 text to video](ltx2/ltx-2-3-dev-txt2video.dgl) · [2.3 image to video](ltx2/ltx-2-3-dev-img2video.dgl) · [2.3 first/last frames](ltx2/ltx-2-3-dev-first-last-frame.dgl) · [2.3 spatial latent upscale](ltx2/ltx-2-3-spatial-upscale.dgl) | | [LTX-2.5](../models/ltx2.md) | [2.5 text to video](ltx2/ltx-2-5-dev-txt2video.dgl) · [2.5 image to video](ltx2/ltx-2-5-dev-img2video.dgl) · [2.5 spatial latent upscale](ltx2/ltx-2-5-spatial-upscale.dgl) | -| [MiniMax-H3](../models/minimax_h3.md) | [Text to audio/video](minimax_h3/minimax-h3-text-to-av.dgl) · [First/last-frame conditioning](minimax_h3/minimax-h3-first-last-frame.dgl) · [Reference to audio/video](minimax_h3/minimax-h3-reference-to-av.dgl) · [Interpolated latent hires](minimax_h3/minimax-h3-latent-hires.dgl) · [Learned latent hires](minimax_h3/minimax-h3-learned-hires.dgl) | +| [MiniMax-H3](../models/minimax_h3.md) | [Text to audio/video](minimax_h3/minimax-h3-text-to-av.dgl) · [First/last-frame conditioning](minimax_h3/minimax-h3-first-last-frame.dgl) · [Reference to audio/video](minimax_h3/minimax-h3-reference-to-av.dgl) · [Interpolated latent hires](minimax_h3/minimax-h3-latent-hires.dgl) · [Learned latent hires](minimax_h3/minimax-h3-learned-hires.dgl) · [AV continuation (two-part seamless chain)](minimax_h3/minimax-h3-av-continuation.dgl) | | [Wan 2.1](../models/wan.md) | [Text to video, 1.3B](wan/wan2-1-t2v-1-3b.dgl) · [Text to video, 14B](wan/wan2-1-t2v-14b.dgl) · [Image to video, 14B](wan/wan2-1-i2v-14b.dgl) · [First/last frames, 14B](wan/wan2-1-first-last-frame-14b.dgl) | | [Wan 2.1 VACE 1.3B](../models/wan.md) | [Text to video](wan/wan2-1-vace-1-3b-txt2video.dgl) · [Reference to video](wan/wan2-1-vace-1-3b-ref2video.dgl) · [Video to video](wan/wan2-1-vace-1-3b-video2video.dgl) | | [Wan 2.1 VACE 14B](../models/wan.md) | [Text to video](wan/wan2-1-vace-14b-txt2video.dgl) · [Reference to video](wan/wan2-1-vace-14b-ref2video.dgl) · [Video to video](wan/wan2-1-vace-14b-video2video.dgl) | From c74fcaffcbcd5b4a9012eb289d889a95cd762e1e Mon Sep 17 00:00:00 2001 From: leejet Date: Sat, 28 Nov 2026 22:50:36 +0800 Subject: [PATCH 6/6] fix(dgl): align and join MiniMax-H3 continuation clips --- docs/dgl/README.md | 2 +- .../minimax_h3/minimax-h3-av-continuation.dgl | 225 ++++++++---------- docs/models/minimax_h3.md | 15 ++ 3 files changed, 110 insertions(+), 132 deletions(-) diff --git a/docs/dgl/README.md b/docs/dgl/README.md index 65b31332fd..c1b96235c0 100644 --- a/docs/dgl/README.md +++ b/docs/dgl/README.md @@ -91,7 +91,7 @@ and [NumPy tensor files](../dgl.md#tensor-files). | [LingBot-Video](../models/lingbot_video.md) | [Dens 1.3B text to video](lingbot_video/lingbot-video-dens-1-3b-txt2video.dgl) | | [LTX-2.3](../models/ltx2.md) | [2.3 text to video](ltx2/ltx-2-3-dev-txt2video.dgl) · [2.3 image to video](ltx2/ltx-2-3-dev-img2video.dgl) · [2.3 first/last frames](ltx2/ltx-2-3-dev-first-last-frame.dgl) · [2.3 spatial latent upscale](ltx2/ltx-2-3-spatial-upscale.dgl) | | [LTX-2.5](../models/ltx2.md) | [2.5 text to video](ltx2/ltx-2-5-dev-txt2video.dgl) · [2.5 image to video](ltx2/ltx-2-5-dev-img2video.dgl) · [2.5 spatial latent upscale](ltx2/ltx-2-5-spatial-upscale.dgl) | -| [MiniMax-H3](../models/minimax_h3.md) | [Text to audio/video](minimax_h3/minimax-h3-text-to-av.dgl) · [First/last-frame conditioning](minimax_h3/minimax-h3-first-last-frame.dgl) · [Reference to audio/video](minimax_h3/minimax-h3-reference-to-av.dgl) · [Interpolated latent hires](minimax_h3/minimax-h3-latent-hires.dgl) · [Learned latent hires](minimax_h3/minimax-h3-learned-hires.dgl) · [AV continuation (two-part seamless chain)](minimax_h3/minimax-h3-av-continuation.dgl) | +| [MiniMax-H3](../models/minimax_h3.md) | [Text to audio/video](minimax_h3/minimax-h3-text-to-av.dgl) · [First/last-frame conditioning](minimax_h3/minimax-h3-first-last-frame.dgl) · [Reference to audio/video](minimax_h3/minimax-h3-reference-to-av.dgl) · [Interpolated latent hires](minimax_h3/minimax-h3-latent-hires.dgl) · [Learned latent hires](minimax_h3/minimax-h3-learned-hires.dgl) · [Audio/video continuation](minimax_h3/minimax-h3-av-continuation.dgl) | | [Wan 2.1](../models/wan.md) | [Text to video, 1.3B](wan/wan2-1-t2v-1-3b.dgl) · [Text to video, 14B](wan/wan2-1-t2v-14b.dgl) · [Image to video, 14B](wan/wan2-1-i2v-14b.dgl) · [First/last frames, 14B](wan/wan2-1-first-last-frame-14b.dgl) | | [Wan 2.1 VACE 1.3B](../models/wan.md) | [Text to video](wan/wan2-1-vace-1-3b-txt2video.dgl) · [Reference to video](wan/wan2-1-vace-1-3b-ref2video.dgl) · [Video to video](wan/wan2-1-vace-1-3b-video2video.dgl) | | [Wan 2.1 VACE 14B](../models/wan.md) | [Text to video](wan/wan2-1-vace-14b-txt2video.dgl) · [Reference to video](wan/wan2-1-vace-14b-ref2video.dgl) · [Video to video](wan/wan2-1-vace-14b-video2video.dgl) | diff --git a/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl b/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl index 6397a4210d..e97b263af5 100644 --- a/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl +++ b/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl @@ -1,24 +1,17 @@ # version: 0.0.0 -# docs/models/minimax_h3.md — AV continuation: chain segment B onto segment A's -# tail latent (video head and audio head pinned as never-denoised), seamless -# multi-part video without pixel-frame anchoring drift. -# -# The overlap is ONE time interval, `overlap_frames` pixel frames of A's tail. -# Video and audio latent counts are both derived from it, so the streams stay -# aligned for any `frames`. To join A and B: decode B, then drop its first -# `overlap_frames` pixel frames of video and the matching overlap duration of -# audio before concatenating (see the join note in main() below — the pinned -# latents land at different positions within Sol-H3's five-latent decoder -# blocks than they occupied in A, so trimming is what makes the seam clean). +# docs/models/minimax_h3.md — Generate and join two audio/video segments with a +# shared latent overlap. var { model_path = "../models/diffusion_models/minimax_h3_fl2va-Q4_K_M.gguf" text_path = "../models/text_encoders/qwen3vl_32b_minimax_h3-Q4_K_M.gguf" vae_path = "../models/vae/minimax_h3_video_vae_fp16.safetensors" audio_vae_path = "../models/vae/minimax_h3_audio_vae_fp32.safetensors" + vae_backend = "default" output_a_path = "continuation-part-a.avi" output_b_path = "continuation-part-b.avi" + output_path = "continuation-joined.avi" latent_a_path = "continuation-part-a.npy" prompt_a = "A cute American Shorthair silver tabby kitten surfs on a tropical ocean wave, cinematic tracking shot, bright sunlight. Upbeat tropical surf-rock background music with cheerful drums and guitar." @@ -29,153 +22,123 @@ var { width = 864 height = 480 frames = 56 - fps = 24 steps = 20 method = "euler" scheduler = "discrete" audio_scale = 4 - # Pixel frames of segment A's tail carried into segment B's head — the - # single time interval both pinned latent counts derive from. 0 means no - # pinning: B degrades to a plain continuation. main() caps the derived - # counts so B always keeps at least half of its latents to generate. - # Default 12 frames at 24 fps = 0.5 s = 4 video latents (17-frame blocks, - # 5 latents each) = 20 audio latents (40 per second). - overlap_frames = 12 + # Maximum overlap in decoded frames. Zero generates two independent clips. + overlap_frames = 22 +} + +fn aligned_overlap(frame_count, requested) { + # Five video latents decode to 17 frames, with a final two-latent block + # producing five frames. A tail of 5 + 17k frames starts on a block boundary. + limit = math::min(requested, math::floor(frame_count / 2)) + aligned = 5 + 17 * math::floor((limit - 5) / 17) + return aligned - math::min(aligned, 0) } -fn pack_av(video, audio, audio_scale) { +# Audio is already scaled in the diffusion latent. The same packing also +# preserves the per-stream mask positions, with ones in unused padding. +fn pack_av(video, audio, pad_value) { shape = tensor::shape(video) count = tensor::numel(audio) tail_channels = math::ceil_div(count, shape[0] * shape[1] * shape[2]) tail_shape = [shape[0], shape[1], shape[2], tail_channels, 1] - flat = tensor::reshape(tensor::mul(audio, audio_scale), [count]) - padded = tensor::pad(flat, [0, math::product(tail_shape) - count]) + flat = tensor::reshape(audio, [count]) + padded = tensor::pad(flat, [0, math::product(tail_shape) - count], value = pad_value) return tensor::concat([video, tensor::reshape(padded, tail_shape)], axis = 3) } -fn unpack_audio(packed, channels, audio_shape, audio_scale) { +fn unpack_audio(packed, channels, audio_shape) { tail = packed[:, :, :, channels:] flat = tensor::reshape(tail, [tensor::numel(tail)]) - return tensor::div(tensor::reshape(flat[:math::product(audio_shape)], audio_shape), audio_scale) + return tensor::reshape(flat[:math::product(audio_shape)], audio_shape) } -# Fold an audio-space pattern [T, 2, 32, 1] into the packed latent's tail -# channels exactly the way pack_av folds audio content, so mask zeros land on -# the same flat positions as the pinned audio frames. -fn audio_pattern_to_tail(audio_pattern, video_shape_tensor) { - count = tensor::numel(audio_pattern) - shape = tensor::shape(video_shape_tensor) - tail_channels = math::ceil_div(count, shape[0] * shape[1] * shape[2]) - tail_shape = [shape[0], shape[1], shape[2], tail_channels, 1] - flat = tensor::reshape(audio_pattern, [count]) - padded = tensor::pad(flat, [0, math::product(tail_shape) - count], value = 1) - return tensor::reshape(padded, tail_shape) +fn continuation_input(latent, video_shape, audio_shape, video_overlap, audio_overlap) { + video_start = video_shape[2] - video_overlap + audio_start = audio_shape[0] - audio_overlap + video_tail = latent[:, :, video_start:, :video_shape[3]] + audio = unpack_audio(latent, video_shape[3], audio_shape) + audio_tail = audio[audio_start:, :, :, :] + + video_pad = [0, 0, 0, 0, 0, video_start] + audio_pad = [0, audio_start] + video_base = tensor::pad(video_tail, video_pad) + audio_base = tensor::pad(audio_tail, audio_pad) + video_mask = tensor::pad(tensor::zeros_like(video_tail), video_pad, value = 1) + audio_mask = tensor::pad(tensor::zeros_like(audio_tail), audio_pad, value = 1) + return {base = pack_av(video_base, audio_base, 0), mask = pack_av(video_mask, audio_mask, 1)} +} + +fn segment_media(decoded_frames, sound, fps, trim_frames) { + frames_kept = tensor::shape(decoded_frames)[3] - trim_frames + sample_start = math::floor(trim_frames * sound.sample_rate / fps) + sample_count = math::floor(frames_kept * sound.sample_rate / fps) + return { + video = {frames = decoded_frames[:, :, :, trim_frames:], fps_num = fps, fps_den = 1}, + audio = {samples = sound.samples[:, sample_start:sample_start + sample_count], sample_rate = sound.sample_rate} + } +} + +fn save_segment(path, vae, audio_vae, latent, channels, audio_shape, audio_scale, fps, trim_frames) { + video_latent = dit_to_vae_latents(vae, latent[:, :, :, :channels]) + decoded_frames = vae_decode(vae, video_latent, tiling = tiling(tile_size_w = 256, tile_size_h = 256)) + audio_latent = tensor::div(unpack_audio(latent, channels, audio_shape), audio_scale) + sound = audio_vae_decode(audio_vae, audio_latent) + media = segment_media(decoded_frames, sound, fps, trim_frames) + video::save(path, media.video, audio = media.audio) + return media } fn main() { - model = load_dit(model_path, family = "minimax_h3", params_backend = "cpu", backend = "vulkan1") - text = load_text_encoder(text_path, family = "minimax_h3", params_backend = "cpu", backend = "vulkan1") - # note: on one test machine (Navi10 dGPU, 8 GiB) the VAE graphs device-lost - # on the default device; routing them to the iGPU's larger unified pool fixed it - vae = load_vae(vae_path, family = "minimax_h3", params_backend = "cpu", backend = "vulkan1") - audio_vae = load_audio_vae(audio_vae_path, family = "minimax_h3", params_backend = "cpu", backend = "vulkan1") - - video_base = tensor::zeros(latent_shape(vae, width = width, height = height, frames = frames)) - audio_zeros = tensor::zeros(audio_latent_shape(audio_vae, frames = frames, fps = fps)) - c_video = tensor::shape(video_base)[3] - t_video = tensor::shape(video_base)[2] - - # ---------- segment A ---------- + model = load_dit(model_path, family = "minimax_h3", params_backend = "cpu") + text = load_text_encoder(text_path, family = "minimax_h3", params_backend = "cpu") + vae = load_vae(vae_path, family = "minimax_h3", backend = vae_backend, params_backend = "cpu") + audio_vae = load_audio_vae(audio_vae_path, family = "minimax_h3", backend = vae_backend, params_backend = "cpu") + + fps = 24 + requested_frames = 5 + (frames - math::min(frames, 5)) + video_shape = latent_shape(vae, width = width, height = height, frames = requested_frames) + video_length = video_shape[2] + frame_count = 4 * video_length - 3 * math::ceil_div(video_length, 5) + # Round up at 40 audio latents per second so the decoded track can be cut + # to the exact video duration even when it ends between audio latents. + audio_length = math::ceil_div(frame_count * 40, fps) + audio_shape = [audio_length, 2, 32, 1] + trim_frames = aligned_overlap(frame_count, overlap_frames) + video_overlap = math::ceil_div(trim_frames * 5, 17) + audio_overlap = math::ceil_div(trim_frames * 40, fps) + condition_a = encode_prompt(text, prompt_a, width = width, height = height) - base_a = pack_av(video_base, audio_zeros, audio_scale = audio_scale) - predict_a = predictor(model, condition_a, audio_length = tensor::shape(audio_zeros)[0]) + predict_a = predictor(model, condition_a, audio_length = audio_length) + base_a = pack_av(tensor::zeros(video_shape), tensor::zeros(audio_shape), 0) sigmas_a = make_sigmas(predict_a, latent = base_a, steps = steps, scheduler = scheduler) - rng = make_rng(rng_type, seed = seed) - start_a = add_noise(predict_a, base_a, tensor::randn_like(base_a, rng = rng), sigma = sigmas_a[0]) - latent_a = sample(start_a, sigmas_a, predict = predict_a, method = method, rng = rng) - - # save after sampling completes (the save point is the final latent, so a - # later refinement pass would naturally run before this line) + rng_a = make_rng(rng_type, seed = seed) + start_a = add_noise(predict_a, base_a, tensor::randn_like(base_a, rng = rng_a), sigma = sigmas_a[0]) + latent_a = sample(start_a, sigmas_a, predict = predict_a, method = method, rng = rng_a) tensor::save(latent_a_path, latent_a) + part_a = save_segment(output_a_path, vae, audio_vae, latent_a, video_shape[3], audio_shape, audio_scale, fps, 0) - frames_a = vae_decode(vae, dit_to_vae_latents(vae, latent_a[:, :, :t_video, :c_video]), - tiling = {enabled = true, tile_size_w = 256, tile_size_h = 256}) - audio_a = audio_vae_decode(audio_vae, unpack_audio(latent_a, channels = c_video, audio_shape = tensor::shape(audio_zeros), audio_scale = audio_scale)) - video::save(output_a_path, {frames = frames_a, fps_num = fps, fps_den = 1}, audio = audio_a) - - # ---------- segment B: continuation ---------- - # Sol-H3 timing: video latents come in five-latent/17-frame blocks after - # the first (video_frames_to_latent_frames: frames <= 5 -> 2 latents, else - # ((frames - 5) / 17) * 5 + 2; decoded_frames(t) = 4t - 3*ceil(t/5)), and - # the audio VAE emits 40 latents per second. Both pinned counts derive - # from the SAME `overlap_frames` pixel-frame interval, so the video and - # audio overlaps always cover the same duration for any `frames`. - packed_prev = tensor::load(latent_a_path) - video_prev = packed_prev[:, :, :, :c_video] - # audio latent is t-major [T, 2, 32, 1]: the tail slice carries whole - # streams, so no flat-offset math is needed even for unequal segments - audio_prev = unpack_audio(packed_prev, channels = c_video, audio_shape = tensor::shape(audio_zeros), audio_scale = audio_scale) - t_audio = tensor::shape(audio_zeros)[0] - t_prev = tensor::shape(video_prev)[2] - ta_prev = tensor::shape(audio_prev)[0] - - h_req = math::ceil_div(overlap_frames * 5, 17) # video latents covering the interval - n_req = math::ceil_div(overlap_frames * 40, fps) # audio latents covering the interval - # Cap the overlap so B keeps room to continue: never carry more than half - # of B's latents on either stream (nor more than A's tail can give). - # overlap_frames = 0 leaves h_req = n_req = 0: the slices below go empty, - # the mask becomes all-ones, and B is a plain continuation — handled - # without special-casing. Positive-index slices are load-bearing here: a - # negative-endpoint slice with a zero count would select the WHOLE tail. - h_cap = math::min(math::min(h_req, math::ceil_div(t_video, 2)), t_prev) - n_cap = math::min(math::min(n_req, math::ceil_div(t_audio, 2)), ta_prev) - video_tail_raw = video_prev[:, :, t_prev - h_cap:, :] - video_tail = video_tail_raw[:, :, -t_video:, :] - audio_tail_raw = audio_prev[ta_prev - n_cap:, :, :, :] - audio_tail = audio_tail_raw[-t_audio:, :, :, :] - h = tensor::shape(video_tail)[2] - n = tensor::shape(audio_tail)[0] - video_base_b = tensor::replace_slice(tensor::zeros(tensor::shape(video_base)), video_tail, axis = 2, start = 0) - audio_b = tensor::replace_slice(tensor::zeros(tensor::shape(audio_zeros)), audio_tail, axis = 0, start = 0) - base_b = pack_av(video_base_b, audio_b, audio_scale = audio_scale) - - # full-width denoise mask over the packed AV latent; zeros pin the carried - # heads exactly (per-step restore: denoised * mask + base * (1 - mask)). - # Audio zeros are folded with the same tail-channel layout as the content. - mask_video = tensor::concat([ - tensor::full([tensor::shape(video_base)[0], tensor::shape(video_base)[1], h, c_video, 1], 0), - tensor::full([tensor::shape(video_base)[0], tensor::shape(video_base)[1], t_video - h, c_video, 1], 1) - ], axis = 2) - mask_audio_pattern = tensor::concat([ - tensor::full([n, tensor::shape(audio_zeros)[1], tensor::shape(audio_zeros)[2], tensor::shape(audio_zeros)[3]], 0), - tensor::full([t_audio - n, tensor::shape(audio_zeros)[1], tensor::shape(audio_zeros)[2], tensor::shape(audio_zeros)[3]], 1) - ], axis = 0) - mask_b = tensor::concat([mask_video, audio_pattern_to_tail(mask_audio_pattern, video_base)], axis = 3) - + continuation = continuation_input(latent_a, video_shape, audio_shape, video_overlap, audio_overlap) condition_b = encode_prompt(text, prompt_b, width = width, height = height) - predict_b = predictor(model, condition_b, audio_length = tensor::shape(audio_zeros)[0]) - sigmas_b = make_sigmas(predict_b, latent = base_b, steps = steps, scheduler = scheduler) + predict_b = predictor(model, condition_b, audio_length = audio_length) + sigmas_b = make_sigmas(predict_b, latent = continuation.base, steps = steps, scheduler = scheduler) rng_b = make_rng(rng_type, seed = seed + 1) - start_b = add_noise(predict_b, base_b, tensor::randn_like(base_b, rng = rng_b), sigma = sigmas_b[0]) + start_b = add_noise(predict_b, continuation.base, tensor::randn_like(continuation.base, rng = rng_b), sigma = sigmas_b[0]) + # H3 uses one noise level across each stream. Anchor the denoised prediction + # so Euler keeps the carried input at the current noise level until sigma zero. latent_b = sample(start_b, sigmas_b, predict = predict_b, method = method, rng = rng_b, - base = base_b, denoise_mask = mask_b, mask_input = true) - - frames_b = vae_decode(vae, dit_to_vae_latents(vae, latent_b[:, :, :t_video, :c_video]), - tiling = {enabled = true, tile_size_w = 256, tile_size_h = 256}) - audio_b_out = audio_vae_decode(audio_vae, unpack_audio(latent_b, channels = c_video, audio_shape = tensor::shape(audio_zeros), audio_scale = audio_scale)) - video::save(output_b_path, {frames = frames_b, fps_num = fps, fps_den = 1}, audio = audio_b_out) - - # Join point: decoded_frames(t) = 4t - 3*ceil(t/5) for Sol-H3, so the h - # pinned head latents reconstruct covered_video frames of A's tail (>= - # overlap_frames). Trim exactly `overlap_frames` from BOTH streams — - # B's first overlap_frames pixel frames of video and the matching - # overlap_frames/fps seconds of audio — then concatenate A. Both trims - # come from the same interval, so the streams stay in sync for any - # `frames`. covered_* are the exact reconstructed counts, for checking. - covered_video = (4 * t_video - 3 * math::ceil_div(t_video, 5)) - (4 * (t_video - h) - 3 * math::ceil_div(t_video - h, 5)) - covered_audio = math::ceil_div(n * fps, 40) - return {video = {frames = frames_b, fps_num = fps, fps_den = 1}, latent = latent_b, audio = audio_b_out, - join = {trim_frames = overlap_frames, covered_video_frames = covered_video, covered_audio_frames = covered_audio}} + base = continuation.base, denoise_mask = continuation.mask) + part_b = save_segment(output_b_path, vae, audio_vae, latent_b, video_shape[3], audio_shape, audio_scale, fps, trim_frames) + + joined_frames = video::join_windows([part_a.video.frames, part_b.video.frames]) + joined_samples = tensor::concat([part_a.audio.samples, part_b.audio.samples], axis = 1) + video = {frames = joined_frames, fps_num = fps, fps_den = 1} + audio = {samples = joined_samples, sample_rate = part_a.audio.sample_rate} + video::save(output_path, video, audio = audio) + return {video = video, audio = audio, latent_b = latent_b, join = {overlap_frames = trim_frames}} } diff --git a/docs/models/minimax_h3.md b/docs/models/minimax_h3.md index 7c2e3bc684..3d15caeda9 100644 --- a/docs/models/minimax_h3.md +++ b/docs/models/minimax_h3.md @@ -133,6 +133,7 @@ Select optional components when required by your checkpoint or task. | [Reference to audio/video](../dgl/minimax_h3/minimax-h3-reference-to-av.dgl) | Use image, video and audio references. | | [Interpolated latent hires](../dgl/minimax_h3/minimax-h3-latent-hires.dgl) | Resize the draft latent, then run a second sampling pass. | | [Learned latent hires](../dgl/minimax_h3/minimax-h3-learned-hires.dgl) | Use a learned latent upscaler between sampling passes. | +| [Audio/video continuation](../dgl/minimax_h3/minimax-h3-av-continuation.dgl) | Generate and join two clips with shared audio and video context. | #### Run a script @@ -156,6 +157,20 @@ to validate syntax and types without loading weights. See the [shared run instructions](../dgl/README.md#run-an-example) for build paths and the server's file-access rules. +#### Audio/video continuation + +The continuation example saves the finished video as `continuation-joined.avi` +at 24 fps. It also writes `continuation-part-a.avi`, +`continuation-part-b.avi` with its overlap removed, and `continuation-part-a.npy` +with A's latent. + +Set `overlap_frames` to the maximum overlap you want. The script rounds down +to a supported overlap and leaves at least half of B for new content. Set it +to zero to generate two independent clips and join them. + +The video and audio VAEs use the default device. Set `vae_backend` to a device +reported by `sd-cli --list-devices` to run both decoders on that device. + #### Latent upscaling Use `load_latent_upscaler` to load the learned spatial upscaler.