Download code/scripts/smoke_pretrained.py from hello-test1/PhotonTTS: direct link, hf CLI and curl.
- Browser
- Download file 3.35 kB
-
https://huggingface.co/hello-test1/PhotonTTS/resolve/main/code/scripts/smoke_pretrained.py
- Command line
-
hf download hf://hello-test1/PhotonTTS/code/scripts/smoke_pretrained.py
-
curl -L -o smoke_pretrained.py https://huggingface.co/hello-test1/PhotonTTS/resolve/main/code/scripts/smoke_pretrained.py
3.35 kB
| """Smoke test a real tier config on GPU: build, train steps on random latents, timed generation. | |
| python scripts/smoke_pretrained.py --config configs/photon_m.yaml --batch 8 --seconds 20 | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import time | |
| import torch | |
| from photontts.config import load_config | |
| from photontts.model.photon import PhotonBatch, PhotonTTS | |
| from photontts.text.tokenizer import build_tokenizer, ctc_targets | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--config", default="configs/photon_m.yaml") | |
| ap.add_argument("--batch", type=int, default=8) | |
| ap.add_argument("--seconds", type=float, default=20.0) | |
| ap.add_argument("--steps", type=int, default=5) | |
| args = ap.parse_args() | |
| cfg = load_config(args.config) | |
| tok = build_tokenizer(cfg.tokenizer) | |
| dev = "cuda" | |
| t0 = time.time() | |
| model = PhotonTTS(cfg, tok.vocab_size).to(dev, torch.bfloat16) | |
| n = lambda m: sum(p.numel() for p in m.parameters()) / 1e6 | |
| print(f"built {cfg.name} in {time.time() - t0:.1f}s | total {n(model):.0f}M | backbone {n(model.backbone):.0f}M " | |
| f"| head {n(model.head):.1f}M | speaker {n(model.speaker):.1f}M | vocab {tok.vocab_size}") | |
| B, Fn = args.batch, int(args.seconds * 25) // 2 * 2 | |
| text = "<mode:speech><lang:en><bw24k>" + "The quick brown fox jumps over the lazy dog. " * int(args.seconds / 3) | |
| ctc = torch.tensor(ctc_targets(text)) | |
| b = PhotonBatch( | |
| text_ids=[torch.tensor(tok.encode(text))] * B, | |
| latents=torch.randn(B, Fn, 64), | |
| latent_lens=torch.full((B,), Fn), | |
| loss_start=torch.zeros(B, dtype=torch.long), | |
| ref_latents=torch.randn(B, 250, 64), ref_mask=torch.ones(B, 250, dtype=torch.bool), | |
| has_ref=torch.ones(B, dtype=torch.bool), | |
| ctc_targets=ctc[None].expand(B, -1).contiguous(), ctc_lens=torch.full((B,), len(ctc)), | |
| ).to(dev) | |
| b.latents, b.ref_latents = b.latents.bfloat16(), b.ref_latents.bfloat16() | |
| opt = torch.optim.AdamW(model.parameters(), lr=1e-5) | |
| model.train() | |
| torch.cuda.reset_peak_memory_stats() | |
| for i in range(args.steps): | |
| torch.cuda.synchronize() | |
| t = time.time() | |
| out = model(b) | |
| out["loss"].backward() | |
| opt.step() | |
| opt.zero_grad(set_to_none=True) | |
| torch.cuda.synchronize() | |
| dt = time.time() - t | |
| audio_s = B * args.seconds | |
| print(f"step {i}: {dt * 1000:.0f} ms | {audio_s / dt:.0f} s-audio/s | " | |
| + " ".join(f"{k}={v.item():.3f}" for k, v in out.items())) | |
| print(f"peak mem {torch.cuda.max_memory_allocated() / 1e9:.1f} GB (bf16 weights, fp32 AdamW states not 8-bit yet)") | |
| model.eval() | |
| ids = torch.tensor(tok.encode("<mode:speech><lang:en>Hello from Photon."), device=dev) | |
| ref = torch.randn(250, 64, device=dev, dtype=torch.bfloat16) | |
| for steps, cfgs in ((8, 1.5), (2, 1.0)): | |
| torch.cuda.synchronize() | |
| t = time.time() | |
| z = model.generate(ids, ref_latents=ref, max_patches=125, min_patches=125, n_steps=steps, cfg_scale=cfgs) | |
| torch.cuda.synchronize() | |
| dt = time.time() - t | |
| print(f"generate {z.shape[0] / 25:.1f}s audio, {steps} head steps, cfg {cfgs}: {dt:.2f}s " | |
| f"-> RTF {dt / (z.shape[0] / 25):.3f} | {dt / 125 * 1000:.1f} ms/patch (eager, no CUDA graphs)") | |
| if __name__ == "__main__": | |
| main() | |