File size: 2,911 Bytes
ab19986
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()