"""Model and Flow-Matching configuration dataclasses for Lua Nova V1.""" from __future__ import annotations from dataclasses import asdict, dataclass import hashlib import json CHECKPOINT_FORMAT = 4 ANNOTATION_VERSION = 3 def _digest(value: object) -> str: return hashlib.sha256(json.dumps(value, sort_keys=True).encode()).hexdigest() @dataclass(frozen=True) class Config: """Acoustic front-end configuration (24 kHz PT-BR, 85-symbol eSpeak-NG inventory).""" sample_rate: int = 24000 hop: int = 256 n_fft: int = 1024 n_mels: int = 80 f0_min: float = 50.0 f0_max: float = 600.0 speakers: tuple[str, ...] = ("F1",) vocab_size: int = 85 channels: int = 192 heads: int = 4 feedforward: int = 640 kernel: int = 7 encoder_layers: int = 4 prosody_layers: int = 2 acoustic_layers: int = 5 dropout: float = 0.1 predictor_channels: int = 128 synth_fft: int = 512 synth_hop: int = 128 noise_fft: int = 256 noise_hop: int = 64 cep_causal: int = 80 cep_anti: int = 16 noise_cep: int = 32 harmonic_limit: float = 0.47 vocoder_type: str = "source_filter" vocos_model: str = "charactr/vocos-mel-24khz" vocos_n_mels: int = 100 max_tokens: int = 512 max_frames: int = 3000 mel_mean: float = -5.0 mel_scale: float = 5.0 energy_mean: float = -4.0 energy_scale: float = 2.0 pitch_reference_hz: float = 200.0 def __post_init__(self) -> None: from .text.phonemizer import SYMBOLS if self.vocab_size != len(SYMBOLS): raise ValueError(f"vocab_size must equal the phoneme inventory size {len(SYMBOLS)}") if self.channels % self.heads or (self.channels // self.heads) % 2: raise ValueError("channels must be divisible by heads with an even head width") if not self.speakers or len(set(self.speakers)) != len(self.speakers): raise ValueError("speaker names must be unique and nonempty") for fft, hop in ((self.synth_fft, self.synth_hop), (self.noise_fft, self.noise_hop)): if fft != 4 * hop or self.hop % hop: raise ValueError("synthesis windows need 4x overlap and a hop dividing the acoustic hop") if self.cep_causal + self.cep_anti >= self.synth_fft // 2 or self.noise_cep >= self.noise_fft // 2: raise ValueError("cepstrum orders must be well below half the synthesis FFT size") if min(self.encoder_layers, self.prosody_layers, self.acoustic_layers, self.kernel) < 1 or not self.kernel % 2: raise ValueError("layer counts must be positive and the kernel odd") if min(self.mel_scale, self.energy_scale) <= 0 or not 0 < self.harmonic_limit < 0.5: raise ValueError("invalid normalization scale or harmonic limit") if self.vocoder_type not in ("source_filter", "vocos") or self.vocos_n_mels < 1: raise ValueError("invalid vocoder_type or vocos_n_mels") def to_dict(self) -> dict: return asdict(self) @classmethod def from_dict(cls, value: dict) -> "Config": return cls(**{**value, "speakers": tuple(value["speakers"])}) @property def fingerprint(self) -> str: return _digest(self.to_dict()) @property def acoustic_config(self) -> dict: """Settings that determine stored wave/mel/prosody tensors.""" fields = ("sample_rate", "hop", "n_fft", "n_mels", "f0_min", "f0_max") return { **{key: getattr(self, key) for key in fields}, "feature_version": 2, "pitch_reference_hz": 200, "log_floor": 1e-5, } @property def acoustic_fingerprint(self) -> str: return _digest(self.acoustic_config) @property def legacy_feature_fingerprint(self) -> str: fields = ("sample_rate", "hop", "n_fft", "n_mels", "f0_min", "f0_max", "speakers") return _digest( { **{key: getattr(self, key) for key in fields}, "vocab_size": 259, "feature_version": 2, "frontend_version": 2, "pitch_reference_hz": 200, "log_floor": 1e-5, } ) ModelConfig = Config @dataclass class CFMConfig: """Conditional Flow Matching + ProsodyFlow configuration.""" base: Config flow_channels: int = 256 flow_layers: int = 6 flow_heads: int = 4 flow_feedforward: int = 768 flow_kernel: int = 5 flow_dropout: float = 0.05 prior_sigma: float = 0.40 cfg_dropout: float = 0.10 use_acoustic_prior: bool = True use_f0_harmonics: bool = True prosody_hidden_dropout: float = 0.15 acoustic_from_frames: bool = False use_energy_gate: bool = False use_ling_context: bool = False use_duration_flow: bool = False use_prosody_flow: bool = False prosody_context_layers: int = 2 dur_flow_channels: int = 128 dur_flow_layers: int = 3 dur_flow_heads: int = 4 dur_flow_feedforward: int = 384 dur_flow_kernel: int = 3 dur_flow_sigma: float = 0.25 dur_flow_steps: int = 4 dur_flow_temp: float = 0.50 prosody_flow_channels: int = 128 prosody_flow_layers: int = 4 prosody_flow_heads: int = 4 prosody_flow_feedforward: int = 384 prosody_flow_kernel: int = 7 prosody_flow_sigma: float = 0.22 prosody_flow_steps: int = 4 prosody_flow_temp: float = 0.75 default_pitch_range: float = 1.0 mel_space: str = "vocos" def to_dict(self) -> dict: d = asdict(self) d["base"] = self.base.to_dict() return d @classmethod def from_dict(cls, data: dict) -> "CFMConfig": d = dict(data) d["base"] = Config.from_dict(d["base"]) d.setdefault("use_acoustic_prior", False) d.setdefault("use_f0_harmonics", False) d.setdefault("prior_sigma", 0.65) d.setdefault("prosody_hidden_dropout", 0.0) d.setdefault("acoustic_from_frames", False) d.setdefault("use_energy_gate", False) d.setdefault("use_ling_context", False) d.setdefault("use_duration_flow", False) d.setdefault("use_prosody_flow", False) d.setdefault("prosody_context_layers", 2) d.setdefault("dur_flow_channels", 128) d.setdefault("dur_flow_layers", 3) d.setdefault("dur_flow_heads", 4) d.setdefault("dur_flow_feedforward", 384) d.setdefault("dur_flow_kernel", 3) d.setdefault("dur_flow_sigma", 0.25) d.setdefault("dur_flow_steps", 4) d.setdefault("dur_flow_temp", 0.50) d.setdefault("prosody_flow_channels", 128) d.setdefault("prosody_flow_layers", 4) d.setdefault("prosody_flow_heads", 4) d.setdefault("prosody_flow_feedforward", 384) d.setdefault("prosody_flow_kernel", 7) d.setdefault("prosody_flow_sigma", 0.22) d.setdefault("prosody_flow_steps", 4) d.setdefault("prosody_flow_temp", 0.75) d.setdefault("default_pitch_range", 1.0) d.setdefault("mel_space", "vocos") return cls(**d)