Download code/tests/test_codec.py from hello-test1/PhotonTTS: direct link, hf CLI and curl.
- Browser
- Download file 2.98 kB
-
https://huggingface.co/hello-test1/PhotonTTS/resolve/main/code/tests/test_codec.py
- Command line
-
hf download hf://hello-test1/PhotonTTS/code/tests/test_codec.py
-
curl -L -o test_codec.py https://huggingface.co/hello-test1/PhotonTTS/resolve/main/code/tests/test_codec.py
2.98 kB
| import math | |
| import pytest | |
| import torch | |
| from photontts.codec.discriminators import CodecDiscriminator | |
| from photontts.codec.losses import MultiResMelLoss, disc_loss, feature_matching_loss, gen_adv_loss | |
| from photontts.codec.semantic_reg import SemanticProjector | |
| from photontts.codec.vae import PhotonCodec, istft_same | |
| def tones(n: int, seconds: float = 1.0, sr: int = 24_000): | |
| """Harmonic tones with varying pitch: a cheap stand-in for voiced audio.""" | |
| t = torch.arange(int(seconds * sr)) / sr | |
| out = [] | |
| for i in range(n): | |
| f0 = 110 * 2 ** (i / 4) | |
| w = sum(torch.sin(2 * math.pi * f0 * k * t) / k for k in range(1, 6)) | |
| out.append(0.3 * w / w.abs().max()) | |
| return torch.stack(out)[:, None] | |
| def test_istft_same_length(): | |
| spec = torch.randn(2, 481, 40, dtype=torch.complex64) | |
| y = istft_same(spec, 960, 240, torch.hann_window(960)) | |
| assert y.shape == (2, 40 * 240) | |
| def test_codec_shapes(tiny_cfg): | |
| codec = PhotonCodec(tiny_cfg.codec) | |
| wav = torch.randn(2, 1, 24_000) | |
| out = codec(wav) | |
| assert out["mean"].shape == (2, 64, 25) # 25 Hz x 64-d | |
| assert out["recon"].shape == wav.shape | |
| assert torch.isfinite(out["kl"]) | |
| def test_codec_pads_partial_frames(tiny_cfg): | |
| codec = PhotonCodec(tiny_cfg.codec) | |
| z = codec.encode(torch.randn(1, 1, 24_000 + 100)) | |
| assert z.shape[-1] == 26 | |
| def test_encoder_is_causal(tiny_cfg): | |
| codec = PhotonCodec(tiny_cfg.codec).eval() | |
| wav = torch.randn(1, 1, 24_000) | |
| z1 = codec.encode(wav) | |
| wav2 = wav.clone() | |
| wav2[..., 12_000:] = torch.randn(1, 1, 12_000) # change the second half only | |
| z2 = codec.encode(wav2) | |
| # frames ending before sample 12000 (frame f covers [960f, 960(f+1))) must be unchanged | |
| assert torch.allclose(z1[..., :12], z2[..., :12], atol=1e-5) | |
| assert not torch.allclose(z1[..., 13:], z2[..., 13:]) | |
| def test_discriminator_and_losses(tiny_cfg): | |
| disc = CodecDiscriminator(ch=8) | |
| real, fake = tones(2), torch.randn(2, 1, 24_000) * 0.1 | |
| rl, rf = disc(real) | |
| fl, ff = disc(fake) | |
| assert len(rl) == 8 | |
| for v in (disc_loss(rl, fl), gen_adv_loss(fl), feature_matching_loss(rf, ff), MultiResMelLoss()(fake, real)): | |
| assert torch.isfinite(v) | |
| def test_semantic_projector(): | |
| proj = SemanticProjector(64, 32, hidden=32) | |
| loss = proj(torch.randn(2, 64, 25), torch.randn(2, 32, 50)) | |
| assert 0.0 <= loss.item() <= 2.0 | |
| def test_codec_overfits_tones(tiny_cfg, device): | |
| torch.manual_seed(0) | |
| codec = PhotonCodec(tiny_cfg.codec).to(device) | |
| mel = MultiResMelLoss().to(device) | |
| wav = tones(4).to(device) | |
| opt = torch.optim.AdamW(codec.parameters(), lr=2e-3) | |
| first = None | |
| for step in range(300): | |
| out = codec(wav) | |
| loss = mel(out["recon"], wav) + 1e-4 * out["kl"] | |
| opt.zero_grad() | |
| loss.backward() | |
| opt.step() | |
| first = first if first is not None else loss.item() | |
| assert loss.item() < 0.5 * first, (first, loss.item()) | |