"""tiny_voca.train_vocoder where the voice-model (paired) crops are graded on a time-smoothed spectrum. Night of 2026-11-26: fitting vocoder C to a texture voice model's pairs (Ctex, Ctex-m2, vtex) cuts the buzz but flattens the vowel motion the texture model adds (T2 + C 0.472 / 0.429 / 0.376 at 1-2 / 2-4 / 4-8 kHz -> 0.44 / 0.40 / 0.35 after fitting). Reason: on a paired crop the input is the voice model's spectrogram, but the mel L1 and the distillation magnitude grade every frame against the REAL recording. The model's frame-to-frame motion does not line up with the recording's, so the frame-exact losses teach the vocoder to ignore it (to output the mean). Here, on paired crops only, both terms compare averages over PAIR_SMOOTH frames (moving average, stride 1): the level and colour per band still have to match the recording (that is what removes the buzz), but frame-level motion is left to the input and the critics. Crops of the recording itself keep the frame-exact losses, which is what teaches the vocoder to pass motion. PAIR_SMOOTH=9 python scripts/train_vocoder_smooth.py It recompiles tiny_voca.train_vocoder with two lines replaced (asserted to exist), so a change upstream fails loudly instead of silently training the original. """ import inspect import os import sys import types import torch import torch.nn.functional as F from tiny_voca import train_vocoder as upstream K = int(os.environ.get('PAIR_SMOOTH', '9')) _seen = set() def _smooth(x, dim): """Moving average of K frames along dim (valid part only).""" x = x.movedim(dim, -1) shape = x.shape y = F.avg_pool1d(x.reshape(-1, 1, shape[-1]), K, 1) return y.reshape(*shape[:-1], y.shape[-1]).movedim(-1, dim) def pair_smoothed_l1(a, b, paired, dim, name): full = (a - b).abs().flatten(1).mean(1) smooth = (_smooth(a, dim) - _smooth(b, dim)).abs().flatten(1).mean(1) use = paired.to(a.device).bool() if name not in _seen: _seen.add(name) print(f'pair smoothing {name}: shape {tuple(a.shape)} time dim {dim} K {K}, paired {int(use.sum())}/{use.numel()}', flush=True) return torch.where(use, smooth, full).mean() def spectrum_distance_paired(student, teacher, paired): (log_s, _), (log_t, _) = student, teacher _, phase = upstream.spectrum_distance(student, teacher) floor, ceiling = upstream.math.log(1e-5), upstream.math.log(1e2) magnitude = pair_smoothed_l1(log_s.clamp(floor, ceiling), log_t.clamp(floor, ceiling), paired, 2, 'distill') return magnitude, phase REPLACE = { ' mel = (features(fake) - real_mel).abs().mean()\n': ' mel = pair_smoothed_l1(features(fake), real_mel, paired, 1, "mel")\n', ' magnitude_part, phase_part = spectrum_distance(predicted, target_spectrum)\n': ' magnitude_part, phase_part = spectrum_distance_paired(predicted, target_spectrum, paired)\n', } def build(): source = inspect.getsource(upstream) for old, new in REPLACE.items(): assert source.count(old) == 1, f'upstream changed: {old.strip()}' source = source.replace(old, new) module = types.ModuleType(upstream.__name__) module.__dict__.update(__file__=upstream.__file__, __package__=upstream.__package__, pair_smoothed_l1=pair_smoothed_l1, spectrum_distance_paired=spectrum_distance_paired) exec(compile(source, upstream.__file__, 'exec'), module.__dict__) return module if __name__ == '__main__': print(f'paired crops graded on {K}-frame averages', flush=True) sys.argv[0] = 'tiny_voca.train_vocoder' build().main()