probe-cp.py
2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""摸清 dit.condition_proj 的溢出幅度:权重/输入/fp32 参考输出的量级。"""
import os, time, torch
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")
from diffsynth.pipelines.minimax_h3_audio_video import MiniMaxH3Pipeline, ModelConfig
DT=torch.float16
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"
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(6.0,free-2.0))
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"))
cp = pipe.dit.condition_proj
print("[cp] type:", type(cp).__name__)
for n,p_ in cp.named_parameters(recurse=True):
print(f" param {n}: shape={tuple(p_.shape)} dtype={p_.dtype} absmax={float(p_.abs().max()):.4g}")
for n,b in cp.named_buffers(recurse=True):
print(f" buf {n}: {tuple(b.shape)} {b.dtype}")
cap={}
def hook(m,i,o):
x=i[0]
cap["x"]=x.detach()
cap["o"]=o.detach() if torch.is_tensor(o) else None
h=cp.register_forward_hook(hook)
try:
pipe(prompt="测试", height=480,width=832,num_frames=124,
num_inference_steps=1, seed=42, flow_shift=6.0)
except Exception as e:
print("[gen] 提前中断(预期):", type(e).__name__, str(e)[:120])
h.remove()
x=cap.get("x")
if x is None:
print("!! 没抓到输入"); raise SystemExit(1)
print(f"\n[输入] shape={tuple(x.shape)} dtype={x.dtype} absmax={float(x.abs().max()):.6g} "
f"mean={float(x.float().mean()):.4g} std={float(x.float().std()):.4g} finite={bool(torch.isfinite(x).all())}")
o=cap.get("o")
if o is not None:
fin=torch.isfinite(o)
print(f"[fp16 输出] absmax={float(o[fin].abs().max()) if fin.any() else float('nan'):.6g} "
f"非有限占比={100*(~fin).float().mean():.2f}%")
# fp32 参考
W=None;B=None
for n,p_ in cp.named_parameters(recurse=True):
if n.endswith("weight"): W=p_
if n.endswith("bias"): B=p_
if W is not None:
ref=torch.nn.functional.linear(x.float(), W.float(), B.float() if B is not None else None)
print(f"[fp32 参考] absmax={float(ref.abs().max()):.6g} (fp16 上限 65504)")
print(f" → 超出 fp16 上限的元素占比 {100*(ref.abs()>65504).float().mean():.4f}%")