diff --git a/docs/dgl/README.md b/docs/dgl/README.md index df8958cc69..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) | +| [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 new file mode 100644 index 0000000000..e97b263af5 --- /dev/null +++ b/docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl @@ -0,0 +1,144 @@ +# version: 0.0.0 +# 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." + 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 + + steps = 20 + method = "euler" + scheduler = "discrete" + audio_scale = 4 + + # 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) +} + +# 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(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) { + tail = packed[:, :, :, channels:] + flat = tensor::reshape(tail, [tensor::numel(tail)]) + return tensor::reshape(flat[:math::product(audio_shape)], audio_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") + 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) + 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_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) + + 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 = 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, 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 = 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.