Download code/tests/test_head.py from hello-test1/PhotonTTS: direct link, hf CLI and curl.
- Browser
- Download file 1.5 kB
-
https://huggingface.co/hello-test1/PhotonTTS/resolve/main/code/tests/test_head.py
- Command line
-
hf download hf://hello-test1/PhotonTTS/code/tests/test_head.py
-
curl -L -o test_head.py https://huggingface.co/hello-test1/PhotonTTS/resolve/main/code/tests/test_head.py
1.5 kB
| import pytest | |
| import torch | |
| from photontts.model.photon_head import PhotonHead | |
| def _head(tiny_cfg, cond_dim=16, frame_dim=0): | |
| return PhotonHead(tiny_cfg.head, 64, 2, cond_dim, frame_cond_dim=frame_dim) | |
| def test_head_shapes_and_zero_init(tiny_cfg): | |
| head = _head(tiny_cfg, frame_dim=8) | |
| N = 5 | |
| v = head(torch.randn(N, 2, 64), torch.rand(N), torch.randn(N, 16), torch.randn(N, 4, 64), | |
| frame_cond=torch.randn(N, 6, 8)) | |
| assert v.shape == (N, 2, 64) | |
| assert v.abs().max() == 0 # adaLN-zero output init | |
| x = head.sample(torch.randn(N, 16), torch.randn(N, 4, 64), n_steps=3, frame_cond=torch.randn(N, 6, 8)) | |
| assert x.shape == (N, 2, 64) | |
| def test_head_overfits_fixed_conditions(tiny_cfg, device): | |
| """4 distinct conditions -> 4 fixed target patches; sampling must recover them.""" | |
| torch.manual_seed(0) | |
| tiny_cfg.head.p_uncond = 0.0 | |
| tiny_cfg.head.ctx_noise = 0.0 | |
| head = _head(tiny_cfg).to(device) | |
| cond = torch.randn(4, 16, device=device) | |
| target = torch.randn(4, 2, 64, device=device) | |
| ctx = torch.zeros(4, 4, 64, device=device) | |
| opt = torch.optim.AdamW(head.parameters(), lr=1e-3) | |
| for _ in range(1500): | |
| loss = head.loss_terms(target, cond, ctx).mean() | |
| opt.zero_grad() | |
| loss.backward() | |
| opt.step() | |
| head.eval() | |
| x = head.sample(cond, ctx, n_steps=16, cfg_scale=1.0, sway=0.0) | |
| err = (x - target).pow(2).mean() / target.pow(2).mean() | |
| assert err < 0.1, err.item() | |