Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse filesFiles changed (2)
- app.py+98-91
- requirements.txt+3-2
123456758 unmodified lines66676869707172737475767778798081821 unmodified line848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471484 unmodified lines1531541551561571581591601611621631641651664 unmodified lines17117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321413 unmodified lines22822923023123223323414 unmodified lines24925025125225325425569 unmodified lines32532632732832933017 unmodified lines348349350351352353354"""MiniMax-H3 video generation with synchronized audio — FL2VA (text / first-last-frame to video+audio).Based on the diffusers MiniMax-H3 pipeline. Uses Int8 weight-only quantization on both the33B transformer and the 32B Qwen3-VL text encoder to fit within ZeroGPU xlarge (96 GB VRAM)."""from __future__ import annotations58 unmodified linesPIPE = NoneLOAD_ERROR: str | None = NoneLOADED_IN: float | None = Nonedef load_models() -> str | None:"""Load the full MiniMax-H3 FL2VA pipeline with Int8 quantization at startup.Int8 weight-only quantization roughly halves VRAM for both the 33B transformer andthe 32B Qwen3-VL text encoder, bringing the total to ~63 GB — fits in xlarge (96 GB)."""global PIPE, LOAD_ERROR, LOADED_INif PIPE is not None or LOAD_ERROR is not None:return LOAD_ERROR1 unmodified linestarted = time.time()try:import torchfrom diffusers import ModularPipeline, MiniMaxH3Transformer3DModel, TorchAoConfigfrom transformers import Qwen3VLForConditionalGenerationfrom transformers import TorchAoConfig as TransformersTorchAoConfigfrom torchao.quantization import Int8WeightOnlyConfiglower_duration_floor()print(f"[load] loading MiniMax-H3 from {MODEL_REPO} with Int8 quantization ...", flush=True)pipe = ModularPipeline.from_pretrained(MODEL_REPO)# Quantize the 33B transformer with Int8 weight-only quantizationpipe.update_components(transformer=MiniMaxH3Transformer3DModel.from_pretrained(MODEL_REPO,subfolder="transformer",dtype=torch.bfloat16,quantization_config=TorchAoConfig(Int8WeightOnlyConfig(version=2),modules_to_not_convert=["proj_in", "audio_proj_in", "context_embedder", "time_embedder", "time_proj","token_refiner", "norm_out", "proj_out", "audio_proj_out",],),),# Quantize the 32B Qwen3-VL text encoder with Int8 weight-only quantizationtext_encoder=Qwen3VLForConditionalGeneration.from_pretrained(MODEL_REPO,subfolder="text_encoder",dtype=torch.bfloat16,quantization_config=TransformersTorchAoConfig(Int8WeightOnlyConfig(version=2),modules_to_not_convert=["model.visual","model.language_model.embed_tokens","model.language_model.norm","lm_head",],),),)pipe.load_components(dtype=torch.bfloat16)# VAEs stay full precision — a bfloat16 audio VAE decodes the soundtrack ~20 dB too quiet.# VAEs are small (~6 GB) so we can pack them at startup; the quantized transformer (~31 GB) and# text encoder (~26 GB) are too large to pack alongside the BF16 download on disk, so they stay# as CPU tensors and move to CUDA on the first GPU call.pipe.vae.to("cuda")pipe.audio_vae.to("cuda")# Use cuDNN fused attention (10-20% faster than SDPA, no extra deps)try:pipe.transformer.set_attention_backend("_native_cudnn")except Exception:pipe.transformer.set_attention_backend("sdpa")PIPE = pipeLOADED_IN = time.time() - startedprint(f"[load] ready in {LOADED_IN:.0f}s", flush=True)except Exception as error:traceback.print_exc()LOAD_ERROR = f"**Loading `{MODEL_REPO}` failed** after {time.time() - started:.0f}s: `{type(error).__name__}: {error}`"4 unmodified linesif LOAD_ERROR:return LOAD_ERRORif PIPE is None:return f"Loading `{MODEL_REPO}` (~119 GB BF16, Int8-quantized at load). Watch the Space logs."return f"Ready · Int8 quantized · loaded in {LOADED_IN:.0f}s"# Duration estimation: linear in rows (matmuls) + quadratic (attention) + decode cost_DUR_B, _DUR_C = 1.5e-4, 5.0e-9_DECODE_BASE, _DECODE_PER_DEFAULT_CANVAS, _DEFAULT_CANVAS_PIXELS = 15, 15, 960 * 544 * 124_PAD = 15def get_duration(prompt, image, last_image, height, width, num_frames, steps, seed, *a, **k):4 unmodified linesrows = latent_frames * patches + (int(image is not None) + int(last_image is not None)) * patchesdenoise = steps * (_DUR_B * rows + _DUR_C * rows**2)decode = _DECODE_BASE + _DECODE_PER_DEFAULT_CANVAS * (height * width * num_frames) / _DEFAULT_CANVAS_PIXELS# Extra allowance for quantized model being somewhat slower per step + cold-start weight transferquant_allowance = 30return max(60, int(denoise + decode) + quant_allowance + _PAD)@spaces.GPU(duration=get_duration, size=GPU_SIZE)def generate(prompt, image, last_image, height, width, num_frames, steps, seed, progress=gr.Progress(track_tqdm=True)):"""Generate video with synchronized audio from text and optional keyframes.Args:prompt: Text description of the video to generate.image: Optional first frame image (PIL Image).last_image: Optional last frame image (PIL Image).height: Output video height in pixels.width: Output video width in pixels.num_frames: Number of frames to generate (must be 17*n+5).steps: Number of denoising steps.seed: Random seed for reproducibility."""import torchfrom diffusers.utils import encode_video# Move the quantized transformer and text encoder to CUDA on each cold worker.# They were kept as CPU tensors at startup to avoid exceeding the 150 GB disk# quota (BF16 download ~119 GB + packed copy would be too large).PIPE.transformer.to("cuda")PIPE.text_encoder.to("cuda")state = PIPE(prompt=prompt,image=image,last_image=last_image,height=int(height),width=int(width),num_frames=int(num_frames),num_inference_steps=int(steps),generator=torch.Generator("cpu").manual_seed(int(seed)),)videos = state.get("videos")audio = state.get("audio")13 unmodified linesdef run_generate(prompt, image_path=None, last_image_path=None, canvas=DEFAULT_CANVAS,duration=5, steps=28, seed=42, progress=gr.Progress(track_tqdm=True)):"""Handle a generation request from the Gradio UI."""if LOAD_ERROR:raise gr.Error(LOAD_ERROR)14 unmodified linesfinal_frame = keyframe(last_image_path) if last_image_path else Noneprogress(0.05, desc=f"Denoising {steps} steps at {width}x{height}, {num_frames} frames ...")path = generate(prompt, first_frame, final_frame, height, width, num_frames, steps, seed, progress)return path69 unmodified lineswith gr.Accordion("Advanced options", open=False):canvas = gr.Dropdown(label="Canvas", choices=list(CANVASES), value=DEFAULT_CANVAS)duration = gr.Slider(label="Duration (s)", minimum=MIN_UI_DURATION, maximum=MAX_UI_DURATION, step=1, value=5)steps = gr.Slider(label="Steps", minimum=10, maximum=40, step=1, value=28)seed = gr.Number(label="Seed", value=42, precision=0)17 unmodified linesrun.click(run_generate,[prompt, image, last_image, canvas, duration, steps, seed],[video],api_name="generate",)123456758 unmodified lines66676869707172737475767778798081828384851 unmodified line878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341354 unmodified lines1401411421431441451461471481491501511521531541551564 unmodified lines16116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321413 unmodified lines22822923023123223323423514 unmodified lines25025125225325425525669 unmodified lines32632732832933033133233333433533633717 unmodified lines355356357358359360361"""MiniMax-H3 video generation with synchronized audio — FL2VA (text / first-last-frame to video+audio).Uses the pruned NVFP4 transformer and local truncated NVFP4-AWQ Qwen3-VL conditioner for compactdownloads (~42 GB total vs 119 GB BF16), fitting in ZeroGPU xlarge (96 GB VRAM)."""from __future__ import annotations58 unmodified linesPIPE = NoneCOND_PIPE = NoneCOND_ERROR: str | None = NoneLOAD_ERROR: str | None = NoneLOADED_IN: float | None = Nonedef load_models() -> str | None:"""Load the compact generator and local truncated conditioner at startup.Uses MiniMaxH3GeneratorBlocks (only VAEs + schedulers + video_processor from MiniMaxAI/MiniMax-H3,~10 GB) plus the pruned NVFP4 transformer from lilcheaty/MiniMax-H3-NVFP4 (~16 GB) and the localNVFP4-AWQ Qwen3-VL conditioner from Comfy-Org/MiniMax-H3 (~16 GB). Total download: ~42 GB."""global PIPE, COND_PIPE, COND_ERROR, LOAD_ERROR, LOADED_INif PIPE is not None or LOAD_ERROR is not None:return LOAD_ERROR1 unmodified linestarted = time.time()try:import torchfrom diffusers import ComponentsManagerfrom h3_split_blocks import MiniMaxH3GeneratorBlockslower_duration_floor()manager = ComponentsManager()blocks = MiniMaxH3GeneratorBlocks()print(f"[gen] loading {[c.name for c in blocks.expected_components]} from {MODEL_REPO} ...", flush=True)pipe = blocks.init_pipeline(MODEL_REPO, components_manager=manager, collection="h3")# Load only VAEs, schedulers, and video_processor from the main repo (~10 GB)pipe.load_components(names=["vae", "audio_vae", "scheduler", "audio_scheduler", "video_processor"],dtype=torch.bfloat16,)# Load the pruned NVFP4 transformer from the separate checkpoint repofrom h3_nvfp4 import load_transformerpipe.update_components(transformer=load_transformer())pipe.transformer.set_attention_backend("_native_cudnn")# Load the local truncated NVFP4-AWQ Qwen3-VL conditionertry:from h3_local_conditioner import load_local_conditionerfrom h3_split_blocks import MiniMaxH3ConditionerBlocksprint("[cond] loading the local truncated NVFP4-AWQ conditioner ...", flush=True)text_encoder, tokenizer, processor = load_local_conditioner()cond_pipe = MiniMaxH3ConditionerBlocks().init_pipeline(MODEL_REPO)cond_pipe.update_components(text_encoder=text_encoder,tokenizer=tokenizer,processor=processor,)except Exception as error:traceback.print_exc()COND_ERROR = f"{type(error).__name__}: {error}"print(f"[cond] local load failed ({COND_ERROR})", flush=True)PIPE, COND_PIPE = pipe, cond_pipeLOADED_IN = time.time() - startedprint(f"[gen] ready in {LOADED_IN:.0f}s", flush=True)except Exception as error:traceback.print_exc()LOAD_ERROR = f"**Loading `{MODEL_REPO}` failed** after {time.time() - started:.0f}s: `{type(error).__name__}: {error}`"4 unmodified linesif LOAD_ERROR:return LOAD_ERRORif PIPE is None:return "Loading pruned NVFP4 transformer + local NVFP4 conditioner + full-precision VAEs (~42 GB). Watch the Space logs."import h3_nvfp4engine_status = h3_nvfp4.status()cond_status = "local NVFP4-AWQ" if COND_PIPE is not None else f"unavailable ({COND_ERROR})"return f"Ready · {engine_status} · VAEs full precision · loaded in {LOADED_IN:.0f}s · conditioner {cond_status}"# Duration estimation_DUR_B, _DUR_C = 1.1745e-4, 3.8396e-9_DECODE_BASE, _DECODE_PER_DEFAULT_CANVAS, _DEFAULT_CANVAS_PIXELS = 15, 15, 960 * 544 * 124_PLACEMENT_ALLOWANCE, _PAD = 12, 10def get_duration(prompt, image, last_image, height, width, num_frames, steps, seed, *a, **k):4 unmodified linesrows = latent_frames * patches + (int(image is not None) + int(last_image is not None)) * patchesdenoise = steps * (_DUR_B * rows + _DUR_C * rows**2)decode = _DECODE_BASE + _DECODE_PER_DEFAULT_CANVAS * (height * width * num_frames) / _DEFAULT_CANVAS_PIXELSlocal_conditioning = 20return max(60, int(denoise + decode) + local_conditioning + _PLACEMENT_ALLOWANCE + _PAD)@spaces.GPU(duration=get_duration, size=GPU_SIZE)def generate(prompt, image, last_image, height, width, num_frames, steps, seed,acceleration="Balanced", progress=gr.Progress(track_tqdm=True)):"""Generate video with synchronized audio from text and optional keyframes."""import torch# Move models to CUDA on each cold workerif COND_PIPE is not None:COND_PIPE.text_encoder.to("cuda")PIPE.to("cuda")# Local conditioningcondition_state = COND_PIPE(prompt=prompt,image=image,last_image=last_image,height=int(height),width=int(width),)prompt_embeds = condition_state.get("prompt_embeds")text_token_tags = condition_state.get("text_token_tags")begin_request = getattr(PIPE.transformer, "begin_request", None)end_request = getattr(PIPE.transformer, "end_request", None)if begin_request is not None:begin_request(int(steps), acceleration)try:with torch.inference_mode():state = PIPE(prompt_embeds=prompt_embeds.to("cuda", non_blocking=True),text_token_tags=text_token_tags,image=image,last_image=last_image,height=int(height),width=int(width),num_frames=int(num_frames),num_inference_steps=int(steps),generator=torch.Generator("cpu").manual_seed(int(seed)),)finally:if end_request is not None:end_request()from diffusers.utils import encode_videovideos = state.get("videos")audio = state.get("audio")13 unmodified linesdef run_generate(prompt, image_path=None, last_image_path=None, canvas=DEFAULT_CANVAS,duration=5, steps=28, seed=42, acceleration="Balanced",progress=gr.Progress(track_tqdm=True)):"""Handle a generation request from the Gradio UI."""if LOAD_ERROR:raise gr.Error(LOAD_ERROR)14 unmodified linesfinal_frame = keyframe(last_image_path) if last_image_path else Noneprogress(0.05, desc=f"Denoising {steps} steps at {width}x{height}, {num_frames} frames ...")path = generate(prompt, first_frame, final_frame, height, width, num_frames, steps, seed, acceleration, progress)return path69 unmodified lineswith gr.Accordion("Advanced options", open=False):canvas = gr.Dropdown(label="Canvas", choices=list(CANVASES), value=DEFAULT_CANVAS)duration = gr.Slider(label="Duration (s)", minimum=MIN_UI_DURATION, maximum=MAX_UI_DURATION, step=1, value=5)acceleration = gr.Radio(label="Acceleration",choices=["Balanced", "Exact"],value="Balanced",info="Balanced uses adaptive step reuse; Exact evaluates every step.",)steps = gr.Slider(label="Steps", minimum=10, maximum=40, step=1, value=28)seed = gr.Number(label="Seed", value=42, precision=0)17 unmodified linesrun.click(run_generate,[prompt, image, last_image, canvas, duration, steps, seed, acceleration],[video],api_name="generate",)
6 unmodified lines78910111213146 unmodified lines# The Qwen3-VL processor decides the vision patch count, so a different minor changes the conditioning.transformers==5.8.0accelerate==1.14.0# Int8 weight-only quantization for the 33B transformer and 32B text encodertorchao==0.18.0# PyAV muxes the generated soundtrack onto the frames (encode_video)avpillow6 unmodified lines7891011121314156 unmodified lines# The Qwen3-VL processor decides the vision patch count, so a different minor changes the conditioning.transformers==5.8.0accelerate==1.14.0# Blackwell-native NVFP4 GEMMs and the fused Q/K RMSNorm + split-half RoPE kernel used by h3_nvfp4.py.# CUDA 13 is mandatory: older builds emulate this path and are slower than BF16.comfy-kitchen==0.2.26# PyAV muxes the generated soundtrack onto the frames (encode_video)avpillow
app.py+98-91
requirements.txt+3-2