File size: 4,563 Bytes
d4101a0 | 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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | """Unit tests for CFMConfig, LuaNovaCFM, Sway Sampling, BigVGAN mel extractor, and checkpoint helpers."""
from __future__ import annotations
from pathlib import Path
import tempfile
import unittest
import torch
from lua_nova_v1.config import CFMConfig, Config
from lua_nova_v1.models import BigVGANMelExtractor, MelStatsHead, LuaNovaCFM
from lua_nova_v1.training.common import export_cfm_checkpoint, load_cfm_model
from lua_nova_v1.utils.checkpoint import soup_checkpoints, strip_legacy_vocoder
class ModelTests(unittest.TestCase):
def _tiny_cfg(self) -> CFMConfig:
base = Config(
channels=32,
heads=2,
feedforward=64,
encoder_layers=1,
prosody_layers=1,
acoustic_layers=1,
predictor_channels=16,
vocos_n_mels=100,
)
return CFMConfig(
base=base,
flow_channels=32,
flow_layers=2,
flow_heads=2,
flow_feedforward=64,
flow_kernel=3,
use_acoustic_prior=True,
use_f0_harmonics=True,
acoustic_from_frames=True,
use_energy_gate=True,
use_ling_context=True,
use_duration_flow=True,
use_prosody_flow=True,
prosody_context_layers=1,
dur_flow_channels=16,
dur_flow_layers=1,
dur_flow_heads=2,
dur_flow_feedforward=32,
prosody_flow_channels=16,
prosody_flow_layers=1,
prosody_flow_heads=2,
prosody_flow_feedforward=32,
mel_space="bigvgan_24k_100",
)
def test_mel_stats_head_requires_no_external_vocos(self) -> None:
model = LuaNovaCFM(self._tiny_cfg())
self.assertIsInstance(model.vocoder, MelStatsHead)
self.assertIsNone(model.vocoder.vocos)
def test_sway_schedule_and_parity(self) -> None:
uniform = LuaNovaCFM.sway_time_grid(8, sway_coef=0.0)
self.assertEqual(uniform, [i / 8 for i in range(9)])
swayed = LuaNovaCFM.sway_time_grid(8, sway_coef=-0.5)
self.assertAlmostEqual(swayed[0], 0.0, places=6)
self.assertAlmostEqual(swayed[-1], 1.0, places=6)
self.assertTrue(all(a < b for a, b in zip(swayed[:-1], swayed[1:])))
self.assertLess(swayed[4], 0.5)
model = LuaNovaCFM(self._tiny_cfg()).eval()
cond = torch.randn(1, 12, 32)
prior = torch.randn(1, 12, 100)
fmask = torch.ones(1, 12, dtype=torch.bool)
f0_f = torch.randn(1, 12, 12)
m1 = model.sample_mel(cond, prior, fmask, n_steps=4, cfg_scale=1.0, temperature=0.35, seed=7, f0_feats=f0_f, sway_coef=0.0)
m2 = model.sample_mel(cond, prior, fmask, n_steps=4, cfg_scale=1.0, temperature=0.35, seed=7, f0_feats=f0_f, sway_coef=-0.5)
self.assertEqual(m1.shape, (1, 12, 100))
self.assertTrue(torch.isfinite(m1).all() and torch.isfinite(m2).all())
def test_bigvgan_mel_extractor_shapes_and_finiteness(self) -> None:
ext = BigVGANMelExtractor().eval()
wave = torch.randn(2, 256 * 20) * 0.1
mel = ext(wave)
self.assertEqual(mel.shape, (2, 20, 100))
self.assertTrue(torch.isfinite(mel).all())
mel_1d = ext(wave[0])
self.assertTrue(torch.allclose(mel[0], mel_1d[0], atol=1e-5))
def test_checkpoint_strip_soup_and_load_roundtrip(self) -> None:
m_a = LuaNovaCFM(self._tiny_cfg()).eval()
m_b = LuaNovaCFM(self._tiny_cfg()).eval()
with tempfile.TemporaryDirectory() as tmp:
p_a = Path(tmp) / "a.pt"
p_b = Path(tmp) / "b.pt"
p_soup = Path(tmp) / "soup.pt"
p_strip = Path(tmp) / "stripped.pt"
export_cfm_checkpoint(m_a, p_a, step=10)
export_cfm_checkpoint(m_b, p_b, step=20)
soup_checkpoints(p_a, p_b, p_soup, alpha=0.5)
strip_legacy_vocoder(p_soup, p_strip, fp16=True)
loaded = load_cfm_model(p_strip, device="cpu")
for k in m_a.state_dict():
if not (k.startswith("vocoder.") and k not in ("vocoder.mel_mean", "vocoder.mel_scale")):
expected = 0.5 * m_a.state_dict()[k] + 0.5 * m_b.state_dict()[k]
if torch.is_floating_point(expected):
self.assertLess((loaded.state_dict()[k] - expected).abs().max().item(), 1e-2)
if __name__ == "__main__":
unittest.main()
|