File size: 1,496 Bytes
7e2301f | 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 | 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)
@pytest.mark.slow
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()
|