File size: 7,303 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""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)