-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
269 lines (226 loc) · 9.14 KB
/
train.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# Ignore warnings
# import warnings
# warnings.filterwarnings("ignore")
# Base
import itertools
from glob import glob
import textgrid
from tqdm import tqdm
import time
from contextlib import nullcontext
import shutil
from pathlib import Path
import math
import random
from tqdm import tqdm
# ML
import torch
import torch.nn.functional as F
from einops import rearrange, reduce, repeat
from accelerate import Accelerator, DistributedDataParallelKwargs
from accelerate.utils import set_seed
import wandb
# Local
from supervoice_enhance.config import config
from supervoice_enhance.model import EnhanceModel
from training.tensors import probability_binary_mask, drop_using_mask
from training.dataset import load_distorted_loader
# Train parameters
train_experiment = "ft-08"
train_project="supervoice-enhance"
train_datasets = ["./external_datasets/hifi-tts/audio"]
train_eval_datasets = ["./external_datasets/libritts-r/test-clean/"]
train_duration = 10
train_source_experiment = None
train_auto_resume = True
train_batch_size = 5 # Per GPU
train_drop_prob = 0.3
train_grad_accum_every = 3
train_steps = 60000
train_loader_workers = 5
train_log_every = 1
train_save_every = 1000
train_watch_every = 1000
train_evaluate_every = 1
train_evaluate_batch_size = 10
train_lr_start = 1e-7
train_lr_max = 2e-5
train_warmup_steps = 5000
train_mixed_precision = "fp16" # "bf16" or "fp16" or None
train_clip_grad_norm = 0.2
train_sigma = 1e-5
# Train
def main():
# Prepare accelerator
ddp_kwargs = DistributedDataParallelKwargs(find_unused_parameters=True)
accelerator = Accelerator(log_with="wandb", kwargs_handlers=[ddp_kwargs], gradient_accumulation_steps = train_grad_accum_every, mixed_precision=train_mixed_precision)
device = accelerator.device
output_dir = Path("./output")
output_dir.mkdir(parents=True, exist_ok=True)
dtype = torch.float16 if train_mixed_precision == "fp16" else (torch.bfloat16 if train_mixed_precision == "bf16" else torch.float32)
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
# set_seed(42) enabling this would force each GPU to have same samples
lr_start = train_lr_start * accelerator.num_processes
lr_max = train_lr_max * accelerator.num_processes
# Prepare dataset
accelerator.print("Loading dataset...")
train_loader = load_distorted_loader(datasets = train_datasets, duration = train_duration, num_workers = train_loader_workers, batch_size = train_batch_size)
# Prepare model
accelerator.print("Loading model...")
step = 0
# Model
flow = torch.hub.load(repo_or_dir='ex3ndr/supervoice-flow', model='flow')
raw_model = EnhanceModel(flow, config)
model = raw_model
wd_params, no_wd_params = [], []
for param in model.parameters():
param_list = no_wd_params if param.ndim < 2 else wd_params
param_list.append(param)
optim = torch.optim.AdamW([{'params': wd_params}, {'params': no_wd_params, 'weight_decay': 0}], lr_max, betas=[0.9, 0.99], weight_decay=0.01, eps=1e-7)
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optim, T_max = train_steps)
# Accelerate
model, optim, train_loader = accelerator.prepare(model, optim, train_loader)
train_cycle = cycle(train_loader)
hps = {
"train_lr_start": train_lr_start,
"train_lr_max": train_lr_max,
"batch_size": train_batch_size,
"grad_accum_every": train_grad_accum_every,
"steps": train_steps,
"warmup_steps": train_warmup_steps,
"mixed_precision": train_mixed_precision,
"clip_grad_norm": train_clip_grad_norm,
}
accelerator.init_trackers(train_project, config=hps)
if accelerator.is_main_process:
wandb.watch(model, log="all", log_freq=train_watch_every * train_grad_accum_every)
# Save
def save():
# Save step checkpoint
fname = str(output_dir / f"{train_experiment}.pt")
fname_step = str(output_dir / f"{train_experiment}.{step}.pt")
torch.save({
# Model
'model': raw_model.state_dict(),
# Optimizer
'step': step,
'optimizer': optim.state_dict(),
'scheduler': scheduler.state_dict(),
}, fname_step)
# Overwrite main checkpoint
shutil.copyfile(fname_step, fname)
# Load
source = None
if (output_dir / f"{train_experiment}.pt").exists():
source = train_experiment
elif train_source_experiment and (output_dir / f"{train_source_experiment}.pt").exists():
source = train_source_experiment
if train_auto_resume and source is not None:
accelerator.print("Resuming training...")
checkpoint = torch.load(str(output_dir / f"{source}.pt"), map_location="cpu")
# Model
raw_model.load_state_dict(checkpoint['model'])
# Optimizer
optim.load_state_dict(checkpoint['optimizer'])
scheduler.load_state_dict(checkpoint['scheduler'])
step = checkpoint['step']
accelerator.print(f'Loaded at #{step}')
# Train step
def train_step():
model.train()
# Update LR
if step < train_warmup_steps:
lr = (lr_start + ((lr_max - lr_start) * step) / train_warmup_steps)
for param_group in optim.param_groups:
param_group['lr'] = lr
lr = lr / accelerator.num_processes
else:
scheduler.step()
lr = scheduler.get_last_lr()[0] / accelerator.num_processes
# Load batch
successful_cycles = 0
failed_steps = 0
while successful_cycles < train_grad_accum_every:
with accelerator.accumulate(model):
with accelerator.autocast():
# Load batch
spec, spec_aug = next(train_cycle)
# Prepare batch
batch_size = spec.shape[0]
seq_len = spec.shape[1]
# Normalize spectograms
spec = (spec - config.audio.norm_mean) / config.audio.norm_std
spec_aug = (spec_aug - config.audio.norm_mean) / config.audio.norm_std
# Prepare target flow (CFM)
times = torch.rand((batch_size,), dtype = spec.dtype, device = device)
t = rearrange(times, 'b -> b 1 1')
source_noise = torch.randn_like(spec, device=device)
noise = (1 - (1 - train_sigma) * t) * source_noise + t * spec
flow = spec - (1 - train_sigma) * source_noise
# Drop mask
if train_drop_prob > 0:
drop_mask = probability_binary_mask(shape = (batch_size,), true_prob = train_drop_prob, device = device)
spec_aug = drop_using_mask(source = spec_aug, replacement = 0, mask = drop_mask)
# Train step
predicted, loss = model(source = spec_aug, noise = noise, times = times, target = flow)
# Backprop
optim.zero_grad()
accelerator.backward(loss)
if accelerator.sync_gradients:
accelerator.clip_grad_norm_(model.parameters(), train_clip_grad_norm)
optim.step()
# Log skipping step
if optim.step_was_skipped:
failed_steps = failed_steps + 1
if torch.isnan(loss).any():
accelerator.print("Step was skipped with NaN loss")
else:
accelerator.print("Step was skipped")
if failed_steps > 20:
raise Exception("Too many failed steps")
else:
successful_cycles = successful_cycles + 1
failed_steps = 0
return loss, predicted, flow, lr
#
# Start Training
#
accelerator.print("Training started at step", step)
while step < train_steps:
start = time.time()
loss, predicted, flow, lr = train_step()
end = time.time()
# Advance
step = step + 1
# Summary
if step % train_log_every == 0 and accelerator.is_main_process:
accelerator.log({
"learning_rate": lr,
"loss": loss,
"predicted/mean": predicted.mean(),
"predicted/max": predicted.max(),
"predicted/min": predicted.min(),
"target/mean": flow.mean(),
"target/max": flow.max(),
"target/min": flow.min()
}, step=step)
accelerator.print(f'Step {step}: loss={loss}, lr={lr}, time={end - start} sec')
# Save
if step % train_save_every == 0 and accelerator.is_main_process:
save()
# End training
if accelerator.is_main_process:
accelerator.print("Finishing training...")
save()
accelerator.end_training()
accelerator.print('✨ Training complete!')
#
# Utility
#
def cycle(dl):
while True:
for data in dl:
yield data
if __name__ == "__main__":
main()