test-fp32.py 2.58 KB
"""关键对照实验:小分辨率下 fp32 端到端能否出片。
若 fp32 出片而 fp16 全黑 → 确认是动态范围问题,而非模型/权重/流程问题。
小分辨率是为了让 fp32 装得下(seq_len 从 ~14100 降到 ~800)。"""
import os, sys, time, torch, numpy as np
BASE="/data/models/minimax-h3"
os.environ.setdefault("DIFFSYNTH_ATTENTION_IMPLEMENTATION","torch")
os.environ.setdefault("DIFFSYNTH_MODEL_BASE_PATH", f"{BASE}/models")
os.environ.setdefault("MODELSCOPE_CACHE", f"{BASE}/.mscache")
os.environ.setdefault("PYTORCH_ALLOC_CONF","expandable_segments:True")
from diffsynth.pipelines.minimax_h3_audio_video import MiniMaxH3Pipeline, ModelConfig

DT = torch.float32 if os.environ.get("TEST_DTYPE","fp32")=="fp32" else torch.float16
H = int(os.environ.get("TEST_H", 256)); W = int(os.environ.get("TEST_W", 448)); F_ = int(os.environ.get("TEST_F", 28))
print(f"[cfg] dtype={DT} {W}x{H} {F_}f", flush=True)
free=torch.cuda.mem_get_info("cuda")[0]/1024**3
vc=dict(offload_dtype=DT,offload_device="cpu",onload_dtype=DT,onload_device="cpu",
        preparing_dtype=DT,preparing_device="cuda",computation_dtype=DT,computation_device="cuda")
NF4="DiffSynth-Studio/MiniMax-H3-NF4"
t0=time.time()
pipe=MiniMaxH3Pipeline.from_pretrained(torch_dtype=DT,device="cuda",
  model_configs=[ModelConfig(model_id=NF4,origin_file_pattern=p,**vc) for p in
    ["minimax-h3-fl2va-nf4.safetensors","minimax-h3-text-encoder-nf4.safetensors",
     "video_vae_nf4.safetensors","audio_vae_nf4.safetensors"]],
  processor_config=ModelConfig(model_id="MiniMax/MiniMax-H3",origin_file_pattern="FL2VA/processor/"),
  vram_limit=max(4.0,free-float(os.environ.get("TEST_RESERVE",5))))
print(f"[load] {time.time()-t0:.0f}s", flush=True)
pipe.load_lora(pipe.dit, ModelConfig(model_id="lightx2v/Minimax-h3-Turbo",
  origin_file_pattern="minimax_h3_fl2v_turbo_4step_v1.0_768p_bf16.safetensors"))

PROMPT=("A girl is very happy, she is speaking in english: "
        "“I enjoy working with Diffsynth-Studio, it's a perfect framework.”")
t1=time.time()
video,audio = pipe(prompt=PROMPT,height=H,width=W,num_frames=F_,
                   num_inference_steps=4,seed=0,flow_shift=6.0)
print(f"[gen ] {time.time()-t1:.0f}s peak={torch.cuda.max_memory_allocated()/1024**3:.1f}G", flush=True)
a=np.stack([np.asarray(f.convert("RGB"),dtype=np.float32) for f in video[::5]])
print(f"[video] frames={len(video)} mean={a.mean():.4f} std={a.std():.4f} max={a.max():.1f}", flush=True)
print(f"[audio] finite={bool(torch.isfinite(audio).all())}", flush=True)
print("[VERDICT] " + ("出片正常 ✅" if a.std()>1.0 else "仍为纯色 ❌"), flush=True)