Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions docs/dgl/minimax_h3/minimax-h3-av-continuation.dgl
Original file line number Diff line number Diff line change
@@ -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}
}
Loading