-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrain_pupum2d.py
More file actions
445 lines (370 loc) · 13.5 KB
/
Copy pathtrain_pupum2d.py
File metadata and controls
445 lines (370 loc) · 13.5 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import os
import json5
import numpy as np
import random
import torch
import torch.nn.functional as F
import argparse
import math
from tqdm import tqdm
from dataloader_webdataset import StreamingAudioWebDataset
from model import PupuM2D
from util import load_config
from librosa.filters import mel as librosa_mel_fn
import accelerate
from torch.optim import AdamW
from accelerate import DistributedDataParallelKwargs
from accelerate.utils import ProjectConfiguration
from accelerate.logging import get_logger
class WarmupCosineSchedule(object):
def __init__(
self,
optimizer,
warmup_steps,
start_lr,
ref_lr,
T_max,
final_lr=0.0,
):
self.optimizer = optimizer
self.start_lr = start_lr
self.ref_lr = ref_lr
self.final_lr = final_lr
self.warmup_steps = warmup_steps
self.T_max = T_max - warmup_steps
self._step = 0.0
def step(self):
self._step += 1
if self._step < self.warmup_steps:
progress = float(self._step) / float(max(1, self.warmup_steps))
new_lr = self.start_lr + progress * (self.ref_lr - self.start_lr)
else:
progress = float(self._step - self.warmup_steps) / float(max(1, self.T_max))
new_lr = max(
self.final_lr,
self.final_lr
+ (self.ref_lr - self.final_lr)
* 0.5
* (1.0 + math.cos(math.pi * progress)),
)
for group in self.optimizer.param_groups:
group["lr"] = new_lr
return new_lr
class CosineWDSchedule(object):
def __init__(self, optimizer, ref_wd, T_max, final_wd=0.0):
self.optimizer = optimizer
self.ref_wd = ref_wd
self.final_wd = final_wd
self.T_max = T_max
self._step = 0.0
def step(self):
self._step += 1
progress = self._step / self.T_max
new_wd = self.final_wd + (self.ref_wd - self.final_wd) * 0.5 * (
1.0 + math.cos(math.pi * progress)
)
if self.final_wd <= self.ref_wd:
new_wd = max(self.final_wd, new_wd)
else:
new_wd = min(self.final_wd, new_wd)
for group in self.optimizer.param_groups:
if ("WD_exclude" not in group) or not group["WD_exclude"]:
group["weight_decay"] = new_wd
return new_wd
class EMASchedule:
def __init__(self, start_ema, end_ema, total_steps):
self.start_ema = start_ema
self.end_ema = end_ema
self.total_steps = total_steps
self.curr_step = 0
def step(self):
if self.curr_step < self.total_steps:
m = self.start_ema + (self.end_ema - self.start_ema) * (
self.curr_step / self.total_steps
)
else:
m = self.end_ema
self.curr_step += 1
return m
def get_pupum2d_param_groups(model):
regular_params = []
no_decay_params = []
for name, param in model.named_parameters():
if not param.requires_grad:
continue
if (
param.ndim <= 1
or "bias" in name
or "norm" in name
or "token" in name
or "embedding" in name
or "pos_embed" in name
):
no_decay_params.append(param)
else:
regular_params.append(param)
return [
{"params": regular_params, "WD_exclude": False},
{"params": no_decay_params, "weight_decay": 0.0, "WD_exclude": True},
]
def pupum2d_loss_fn(pred, target, norm_target: bool = True):
if norm_target:
mean = target.mean(dim=-1, keepdim=True)
var = target.var(dim=-1, keepdim=True)
target = (target - mean) / (var + 1e-6).sqrt()
loss = F.smooth_l1_loss(pred, target)
return loss
@torch.no_grad()
def update_ema_teacher(student_params, teacher_params, momentum):
for param_s, param_t in zip(student_params, teacher_params):
param_t.data.mul_(momentum).add_(param_s.data, alpha=1.0 - momentum)
def force_zero_grad(optimizer):
optimizer.zero_grad()
inner_optimizer = getattr(optimizer, "optimizer", None)
if inner_optimizer is not None:
try:
inner_optimizer.zero_grad(set_to_none=True)
except TypeError:
inner_optimizer.zero_grad()
mel_basis = {}
hann_window = {}
@torch.no_grad()
def extract_mel_features(y, cfg, center=False):
global mel_basis, hann_window
if cfg.preprocess.fmax not in mel_basis:
mel = librosa_mel_fn(
sr=cfg.preprocess.sample_rate,
n_fft=cfg.preprocess.n_fft,
n_mels=cfg.preprocess.n_mels,
fmin=cfg.preprocess.fmin,
fmax=cfg.preprocess.fmax,
)
mel_basis[str(cfg.preprocess.fmax) + "_" + str(y.device)] = (
torch.from_numpy(mel).float().to(y.device)
)
hann_window[str(y.device)] = torch.hann_window(cfg.preprocess.win_size).to(
y.device
)
y = torch.nn.functional.pad(
y.unsqueeze(1),
(
int((cfg.preprocess.n_fft - cfg.preprocess.hop_size) / 2),
int((cfg.preprocess.n_fft - cfg.preprocess.hop_size) / 2),
),
mode="reflect",
)
y = y.squeeze(1)
spec = torch.stft(
y,
cfg.preprocess.n_fft,
hop_length=cfg.preprocess.hop_size,
win_length=cfg.preprocess.win_size,
window=hann_window[str(y.device)],
center=center,
pad_mode="reflect",
normalized=False,
onesided=True,
return_complex=True,
)
spec = torch.view_as_real(spec)
spec = torch.sqrt(spec.pow(2).sum(-1) + (1e-9))
spec = torch.matmul(mel_basis[str(cfg.preprocess.fmax) + "_" + str(y.device)], spec)
spec = torch.log(torch.clamp(spec, min=1e-5))
if cfg.preprocess.normalize:
mean = -4.089994845986366
std = 2.0242277159094813
spec = (spec - mean) / (std + 1e-8)
if cfg.preprocess.flip_ft:
spec = spec.transpose(-2, -1)
return spec.unsqueeze(1)
def cuda_relevant():
torch.cuda.empty_cache()
torch.backends.cudnn.benchmark = True
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
def dump_cfg(cfg, path):
os.makedirs(os.path.dirname(path), exist_ok=True)
json5.dump(
cfg,
open(path, "w"),
indent=4,
sort_keys=True,
ensure_ascii=False,
quote_keys=True,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", default="config.json", required=True)
parser.add_argument("--exp_name", type=str, required=True)
parser.add_argument("--resume_type", type=str)
parser.add_argument("--checkpoint", type=str)
parser.add_argument("--log_level", default="warning")
args = parser.parse_args()
cfg = load_config(args.config)
cuda_relevant()
cfg.exp_name = args.exp_name
ddp_kwargs = DistributedDataParallelKwargs()
exp_dir = os.path.join(os.path.abspath(cfg.log_dir), args.exp_name)
project_config = ProjectConfiguration(
project_dir=exp_dir, logging_dir=os.path.join(exp_dir, "log")
)
accelerator = accelerate.Accelerator(
gradient_accumulation_steps=cfg.train.gradient_accumulation_step,
log_with=cfg.train.tracker,
project_config=project_config,
kwargs_handlers=[ddp_kwargs],
)
if accelerator.is_main_process:
os.makedirs(project_config.project_dir, exist_ok=True)
os.makedirs(project_config.logging_dir, exist_ok=True)
with accelerator.main_process_first():
accelerator.init_trackers(args.exp_name)
logger = get_logger(args.exp_name, log_level=args.log_level)
checkpoint_dir = os.path.join(exp_dir, "checkpoint")
if accelerator.is_main_process:
os.makedirs(checkpoint_dir, exist_ok=True)
step = 0
with accelerator.main_process_first():
random.seed(cfg.train.random_seed)
np.random.seed(cfg.train.random_seed)
torch.random.manual_seed(cfg.train.random_seed)
with accelerator.main_process_first():
train_dataset = StreamingAudioWebDataset(
"TBD", cfg
)
train_dataloader = train_dataset.get_dataloader()
with accelerator.main_process_first():
model = PupuM2D(cfg)
with accelerator.main_process_first():
param_groups = get_pupum2d_param_groups(model)
optimizer = AdamW(
param_groups,
lr=cfg.train.optimizer.start_lr,
betas=(cfg.train.optimizer.adam_b1, cfg.train.optimizer.adam_b2),
eps=1e-6,
)
lr_scheduler = WarmupCosineSchedule(
optimizer,
warmup_steps=cfg.train.warmup_steps,
start_lr=cfg.train.optimizer.start_lr,
ref_lr=cfg.train.optimizer.peak_lr,
final_lr=cfg.train.optimizer.final_lr,
T_max=cfg.train.total_steps,
)
wd_scheduler = CosineWDSchedule(
optimizer,
ref_wd=cfg.train.optimizer.start_wd,
final_wd=cfg.train.optimizer.final_wd,
T_max=cfg.train.total_steps,
)
ema_scheduler = EMASchedule(
start_ema=cfg.train.optimizer.start_ema,
end_ema=cfg.train.optimizer.final_ema,
total_steps=cfg.train.total_steps,
)
train_dataloader, model, optimizer = accelerator.prepare(
train_dataloader, model, optimizer
)
with accelerator.main_process_first():
if args.resume_type == "resume":
accelerator.load_state(args.checkpoint)
try:
step = int(args.checkpoint.split("step-")[-1].split("_")[0]) + 1
if step > 0:
logger.info(
f"Resumed from step {step}, Fast-forwarding schedulers..."
)
lr_scheduler._step = float(step - 1)
wd_scheduler._step = float(step - 1)
ema_scheduler.curr_step = step - 1
lr_scheduler.step()
wd_scheduler.step()
except:
logger.warning("Could not parse epoch/step from checkpoint path")
config_save_path = os.path.join(exp_dir, "args.json")
if accelerator.is_main_process:
dump_cfg(cfg, config_save_path)
model.train()
optimizer.zero_grad()
train_iter = iter(train_dataloader)
progress_bar = tqdm(
range(step, cfg.train.total_steps),
desc="Training",
disable=not accelerator.is_main_process,
initial=step,
total=cfg.train.total_steps,
dynamic_ncols=True,
)
curr_lr = optimizer.param_groups[0]["lr"]
curr_wd = cfg.train.optimizer.start_wd
momentum = cfg.train.optimizer.start_ema
accumulated_loss = 0.0
while step < cfg.train.total_steps:
try:
batch = next(train_iter)
except StopIteration:
train_iter = iter(train_dataloader)
batch = next(train_iter)
except Exception as e:
logger.warning(f"Batch read error: {e}")
continue
with accelerator.accumulate(model):
audio_gt = batch.to(accelerator.device).squeeze()
mel_gt = extract_mel_features(audio_gt, cfg)
with accelerator.autocast():
z_pred, h_target = model(mel_gt, step)
loss = pupum2d_loss_fn(
z_pred, h_target, norm_target=cfg.train.norm_pix_loss
)
if torch.isnan(loss) or torch.isinf(loss):
logger.error(f"Loss NaN/Inf at step {step}!")
force_zero_grad(optimizer)
accumulated_loss = 0.0
continue
accumulated_loss += loss.item() / cfg.train.gradient_accumulation_step
accelerator.backward(loss)
if accelerator.sync_gradients:
accelerator.clip_grad_norm_(model.parameters(), cfg.train.clip_grad)
optimizer.step()
optimizer.zero_grad()
if not accelerator.sync_gradients:
continue
curr_lr = lr_scheduler.step()
curr_wd = wd_scheduler.step()
unwrapped_model = accelerator.unwrap_model(model)
momentum = ema_scheduler.step()
update_ema_teacher(
unwrapped_model.student.parameters(),
unwrapped_model.teacher.parameters(),
momentum,
)
loss_val = accumulated_loss
accumulated_loss = 0.0
if step % 100 == 0:
accelerator.log(
{
"Train/Loss": loss_val,
"Train/LR": curr_lr,
"Train/WD": curr_wd,
"Train/EMA": momentum,
},
step=step,
)
if accelerator.is_main_process:
progress_bar.set_postfix(
{
"loss": f"{loss_val:.4f}",
"lr": f"{curr_lr:.2e}",
"wd": f"{curr_wd:.3f}",
}
)
step = step + 1
progress_bar.update(1)
if step % cfg.train.save_checkpoint_stride == 0 and step > 0:
if accelerator.is_main_process:
save_path = os.path.join(
checkpoint_dir,
"step-{:07d}_loss-{:.6f}".format(step, loss_val),
)
accelerator.save_state(save_path)
accelerator.end_training()