-
Notifications
You must be signed in to change notification settings - Fork 25
/
train_s2a.py
214 lines (184 loc) · 7.69 KB
/
train_s2a.py
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""S2A training logic.
Copyright PolyAI Limited.
"""
import argparse
import json
import os
from pathlib import Path
from typing import List
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import LearningRateMonitor, ModelCheckpoint
from pytorch_lightning.loggers import TensorBoardLogger, WandbLogger
from data.data_module import DataModule
from modules.s2a_model import Pheme
from modules.vocoder import VocoderType
def parse_args():
parser = argparse.ArgumentParser()
# Paths
parser.add_argument("--saving_path", type=str, default="./ckpt")
parser.add_argument("--resume_checkpoint", type=str, default=None)
parser.add_argument(
"--vocoder_type",
type=str,
choices=[voc_type.name for voc_type in VocoderType],
default=VocoderType.SPEECHTOKENIZER.name,
)
parser.add_argument("--vocoder_config_path", type=str, default=None)
parser.add_argument("--vocoder_ckpt_path", type=str, default=None)
parser.add_argument(
"--metapath", type=str, nargs="+", help="paths to train metadata",
required=True
)
parser.add_argument(
"--val_metapath", type=str, nargs="+", default=[],
help="paths to validation metadata",
)
parser.add_argument("--pretrained_path", type=str, default=None)
parser.add_argument("--speaker_embedding_dir", type=str, default=None)
parser.add_argument("--sampledir", type=str, default="./logs")
# Optimizer
parser.add_argument("--lr", type=float, default=1e-4)
parser.add_argument("--batch_size", type=float, default=200)
parser.add_argument("--max_length", type=int, default=1600)
parser.add_argument("--train_bucket_size", type=int, default=8192)
parser.add_argument("--training_step", type=int, default=800000)
parser.add_argument("--optim_flat_percent", type=float, default=0.0)
parser.add_argument("--warmup_step", type=int, default=50)
parser.add_argument("--adam_beta1", type=float, default=0.9)
parser.add_argument("--adam_beta2", type=float, default=0.98)
# Architecture
parser.add_argument("--ffd_size", type=int, default=3072)
parser.add_argument("--hidden_size", type=int, default=768)
parser.add_argument("--enc_nlayers", type=int, default=6)
parser.add_argument("--dec_nlayers", type=int, default=6)
parser.add_argument("--nheads", type=int, default=12)
parser.add_argument("--dropout", type=float, default=0.1)
parser.add_argument("--depthwise_conv_kernel_size", type=int, default=5)
parser.add_argument("--aligner_softmax_temp", type=float, default=1.0)
parser.add_argument("--layer_norm_eps", type=float, default=1e-5)
parser.add_argument("--use_sem_tokens", type=bool, default=True)
parser.add_argument("--use_spkr_emb", action="store_true")
parser.add_argument("--use_text_emb", action="store_true")
parser.add_argument("--only_inference", action="store_true")
# Dropout
parser.add_argument("--speaker_embed_dropout", type=float, default=0.05)
parser.add_argument("--label_smoothing", type=float, default=0.0)
# Trainer
parser.add_argument("--val_check_interval", type=int, default=1)
parser.add_argument("--max_dataset_samples", type=int, default=-1)
parser.add_argument("--check_val_every_n_epoch", type=int, default=1)
parser.add_argument(
"--precision", type=str, choices=["16", "32", "bf16"], default="bf16"
)
parser.add_argument("--nworkers", type=int, default=16)
parser.add_argument("--distributed", action="store_true")
parser.add_argument(
"--accelerator",
type=str,
choices=["cpu", "gpu", "tpu", "ipu", "hpu", "mps", "auto"],
default="gpu",
)
parser.add_argument("--version", type=int, default=None)
parser.add_argument("--accumulate_grad_batches", type=int, default=1)
# Data
parser.add_argument("--sample_rate", type=int, default=16000)
parser.add_argument("--n_codes", type=int, default=1024)
parser.add_argument("--n_cluster_groups", type=int, default=7)
parser.add_argument("--first_n_lvls", type=int, default=7)
parser.add_argument("--use_pretrained_ckpt_cfg", action="store_true")
parser.add_argument("--n_semantic_codes", type=int, default=1024)
# Distribution
parser.add_argument("--sagemaker", action="store_true")
args = parser.parse_args()
return args
def split_metapath(in_paths: List[str]):
podidx_paths, other_paths = [], []
for itm_path in in_paths:
if itm_path.endswith("jsonl"):
podidx_paths.append(itm_path)
else:
other_paths.append(itm_path)
return podidx_paths, other_paths
if __name__ == "__main__":
args = parse_args()
os.makedirs(args.saving_path, exist_ok=True)
with open(os.path.join(args.saving_path, "config.json"), "w") as f:
json.dump(args.__dict__, f, indent=2)
if args.pretrained_path:
if (
Path(args.pretrained_path).with_name("config.json").exists()
and args.use_pretrained_ckpt_cfg
):
with open(
Path(args.pretrained_path).with_name("config.json"), "r") as f:
prev_cfg = json.load(f)
for k, v in prev_cfg.items():
if isinstance(v, (int,)):
if args.__dict__[k] != v:
print(f"updating {k}!", args.__dict__[k], v)
args.__dict__[k] = v
fname_prefix = f""
checkpoint_callback = ModelCheckpoint(
dirpath=args.saving_path,
filename=(fname_prefix + "{epoch}-{step}"),
every_n_train_steps=(
None if args.val_check_interval == 1.0 else args.val_check_interval # noqa
),
every_n_epochs=(
None if args.check_val_every_n_epoch == 1 else args.check_val_every_n_epoch # noqa
),
verbose=True,
save_last=True,
save_top_k=3,
monitor="val/dataset_0/acc_top_5",
mode='max'
)
lr_monitor = LearningRateMonitor(logging_interval="step")
logger_tb = TensorBoardLogger(
args.saving_path, name="VQ-TTS", version=args.version)
logger_wandb = WandbLogger(project="mqtts", log_model=True, config=args)
distribution_kwargs = {
"accelerator": "gpu",
"strategy": "ddp_find_unused_parameters_true" if args.distributed else "auto", # noqa
}
wrapper = Trainer(
precision=args.precision,
callbacks=[checkpoint_callback, lr_monitor],
num_sanity_val_steps=20,
max_steps=args.training_step,
accumulate_grad_batches=args.accumulate_grad_batches,
logger=[logger_tb, logger_wandb],
check_val_every_n_epoch=args.check_val_every_n_epoch,
profiler="simple",
use_distributed_sampler=False,
# distribution
**distribution_kwargs,
)
model = Pheme(args)
logger_wandb.watch(model=model)
_, other_metapath = split_metapath(args.metapath)
_, other_val_metapath = split_metapath(args.val_metapath)
print(
f"Received datasets: \n{other_metapath = } "
f"\n \n{other_val_metapath = }"
)
other_meta = {}
if len(other_metapath) > 0:
other_meta["fit"] = other_metapath
if len(other_val_metapath) > 0:
other_meta["valid"] = other_val_metapath
data_module = DataModule(
args, other_metapath, other_val_metapath,
wrapper.world_size, wrapper.local_rank
)
data_module.setup(stage="fit")
train_data_module = data_module
valid_dataloaders = []
data_module.setup(stage="valid")
valid_dataloaders.extend(data_module.val_dataloader())
wrapper.fit(
model,
train_dataloaders=train_data_module.train_dataloader(),
val_dataloaders=valid_dataloaders,
ckpt_path=args.resume_checkpoint,
)