zziang commited on
Commit
ff5b803
·
verified ·
1 Parent(s): e6e274e

add structure probe for the browser export

Browse files
Files changed (1) hide show
  1. kaggle/describe_model.py +112 -0
kaggle/describe_model.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Print the model's internal structure so the ONNX export can be written
2
+ against real APIs instead of guesses.
3
+
4
+ The original export notebook is gone, and the graphs it produced died with it.
5
+ Everything downstream (browser bundle, quantization) hangs off being able to
6
+ re-export, so this dumps the exact callable signatures, module classes and
7
+ parameter shapes needed to write that exporter once and correctly.
8
+
9
+ Writes /kaggle/working/structure.json and prints a readable summary.
10
+ """
11
+ import inspect
12
+ import json
13
+ import sys
14
+ from pathlib import Path
15
+
16
+ ROOT = Path("/kaggle/working")
17
+ REPO = ROOT / "Ming-3.0-tts"
18
+ MODEL = ROOT / "Ming-3.0-tts-0.5b"
19
+ OUT = ROOT / "structure.json"
20
+
21
+
22
+ def describe(name: str, fn) -> dict:
23
+ try:
24
+ sig = str(inspect.signature(fn))
25
+ except Exception:
26
+ sig = "<no signature>"
27
+ return {"name": name, "type": type(fn).__name__, "signature": sig,
28
+ "doc": (inspect.getdoc(fn) or "")[:400]}
29
+
30
+
31
+ def main() -> None:
32
+ import torch
33
+
34
+ sys.path.insert(0, str(REPO))
35
+ for name in ("ming_tts.py", "modeling_bailingmm.py"):
36
+ p = REPO / name
37
+ s = p.read_text()
38
+ s = s.replace("torch_dtype=torch.bfloat16", "torch_dtype=torch.float32")
39
+ s = s.replace(".eval().to(torch.bfloat16).to(device)", ".eval().to(torch.float32).to(device)")
40
+ s = s.replace("dtype=torch.bfloat16", "dtype=torch.float32")
41
+ p.write_text(s)
42
+
43
+ from ming_tts import MingTTS
44
+ import tts_tasks
45
+
46
+ tts = MingTTS(str(MODEL), device="cuda", text_normalization=False)
47
+ mm = tts.model
48
+
49
+ info = {"mm_class": type(mm).__name__, "attrs": [a for a in dir(mm) if not a.startswith("_")][:80]}
50
+
51
+ lm = getattr(mm, "model", None)
52
+ info["lm_class"] = type(lm).__name__ if lm is not None else None
53
+ if lm is not None:
54
+ cfg = getattr(lm, "config", None)
55
+ info["lm_config"] = {k: v for k, v in vars(cfg).items()
56
+ if isinstance(v, (int, float, str, bool, list))} if cfg else None
57
+ info["lm_forward"] = describe("lm.forward", lm.forward)
58
+
59
+ for attr in ("linear_proj_audio", "prepare_input_embed", "prepare_prompt", "synthesize"):
60
+ if hasattr(mm, attr):
61
+ info[attr] = describe(attr, getattr(mm, attr))
62
+ if hasattr(tts, attr):
63
+ info[f"tts.{attr}"] = describe(attr, getattr(tts, attr))
64
+
65
+ audio = getattr(mm, "audio", None)
66
+ info["audio_class"] = type(audio).__name__ if audio is not None else None
67
+ for m in ("encode", "decode", "quantize", "dequantize"):
68
+ if audio is not None and hasattr(audio, m):
69
+ info[f"audio.{m}"] = describe(m, getattr(audio, m))
70
+ if audio is not None:
71
+ cfg = getattr(audio, "config", None)
72
+ info["audio_config"] = {k: v for k, v in vars(cfg).items()
73
+ if isinstance(v, (int, float, str, bool))} if cfg else None
74
+
75
+ flow = getattr(mm, "flowloss", None)
76
+ info["flow_class"] = type(flow).__name__ if flow is not None else None
77
+ if flow is not None:
78
+ info["flow_attrs"] = [a for a in dir(flow) if not a.startswith("_")]
79
+ dit = getattr(getattr(flow, "cfm", None), "model", None)
80
+ if dit is not None:
81
+ info["dit_forward"] = describe("dit.forward", dit.forward)
82
+
83
+ totals = {}
84
+ for name, mod in (("lm", lm), ("audio", audio), ("flow", flow)):
85
+ if mod is not None and hasattr(mod, "parameters"):
86
+ totals[name] = sum(p.numel() for p in mod.parameters())
87
+ if hasattr(mm, "linear_proj_audio") and hasattr(mm.linear_proj_audio, "parameters"):
88
+ totals["linear_proj_audio"] = sum(p.numel() for p in mm.linear_proj_audio.parameters())
89
+ info["param_counts"] = totals
90
+
91
+ heads = {}
92
+ for h in ("stop_head", "spk_head", "fc1"):
93
+ for owner, label in ((mm, f"mm.{h}"), (audio, f"audio.{h}")):
94
+ if owner is not None and hasattr(owner, h):
95
+ mod = getattr(owner, h)
96
+ n = sum(p.numel() for p in mod.parameters()) if hasattr(mod, "parameters") else None
97
+ heads[label] = n
98
+ info["heads"] = heads
99
+
100
+ OUT.write_text(json.dumps(info, ensure_ascii=False, indent=1))
101
+ print(json.dumps({k: v for k, v in info.items() if k in
102
+ ("mm_class", "lm_class", "audio_class", "flow_class",
103
+ "param_counts", "heads", "lm_config", "audio_config")},
104
+ ensure_ascii=False, indent=1)[:4000])
105
+ print("\nsignatures:")
106
+ for k, v in info.items():
107
+ if isinstance(v, dict) and "signature" in v:
108
+ print(f" {k}{v['signature']}")
109
+
110
+
111
+ if __name__ == "__main__":
112
+ main()