alpa / scripts /train_voice_rows.py
77ethers's picture
The code that made Alpa: conversion, data preparation, trainer, four helper scripts and TRAINING.md
ab19986 verified
Raw History Blame Contribute Delete
2.91 kB
"""Train only one voice's two rows (2 x 192 numbers) with the repository's trainer; everything else stays as it is.
python scripts/train_voice_rows.py female --train ... --validation ... --output ... --init-from last.pt --new-data ...
The first argument is the voice; the rest are alpa.train's own options. After every step the other voices' rows are
put back to their saved values (the optimiser's weight decay would otherwise shrink them), and at the end the
script reports which tensors changed. For Alpa this moved the loss on unseen sentences by 2%: a voice's rows alone
cannot teach the rest of the network a new speaker (docs/report, Section 8.2). It is the safe first step, not the last.
"""
import sys
import torch
import alpa.train as trainer
TABLES = ("model.voices.weight", "model.frame_voices.weight")
def main():
if len(sys.argv) < 2 or sys.argv[1].startswith("-"):
print(__doc__)
raise SystemExit(0 if sys.argv[1:2] in (["-h"], ["--help"]) else 2)
voice, sys.argv = sys.argv[1], [sys.argv[0], *sys.argv[2:]]
kept = {}
original = trainer.optimize_step
def step_rows_only(system, critics, optimizer, critic_optimizer, batch, step, recipe):
if not kept:
speakers = list(system.model.config.speakers)
if voice not in speakers:
raise SystemExit(f"unknown voice {voice!r}; this model has {speakers}")
kept["row"] = speakers.index(voice)
for name, parameter in system.named_parameters():
parameter.requires_grad_(name in TABLES)
kept["start"] = {name: value.detach().clone() for name, value in system.state_dict().items()}
kept["system"] = system
out = original(system, critics, optimizer, critic_optimizer, batch, step, recipe)
with torch.no_grad():
parameters = dict(system.named_parameters())
for name in TABLES:
others = [row for row in range(parameters[name].shape[0]) if row != kept["row"]]
parameters[name][others] = kept["start"][name][others]
return out
trainer.optimize_step = step_rows_only
try:
trainer.main()
finally:
if kept:
now, row = kept["system"].state_dict(), kept["row"]
changed = [name for name, value in kept["start"].items() if not torch.equal(value, now[name])]
others_same = all(torch.equal(torch.cat((kept["start"][name][:row], kept["start"][name][row + 1:])), torch.cat((now[name][:row], now[name][row + 1:]))) for name in TABLES)
moved = {name: round(float((now[name][row] - kept["start"][name][row]).norm() / kept["start"][name][row].norm()), 4) for name in TABLES}
print({"tensors_that_changed": changed, "other_voices_identical": others_same, "relative_change_of_the_trained_rows": moved}, flush=True)
if __name__ == "__main__":
main()