-
Notifications
You must be signed in to change notification settings - Fork 109
/
train.py
450 lines (402 loc) · 19.1 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
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
446
447
448
449
450
import os
import sys
os.environ['HF_HUB_CACHE'] = './checkpoints/hf_cache'
import torch
import torch.multiprocessing as mp
import random
import librosa
import yaml
import argparse
import torchaudio
import torchaudio.compliance.kaldi as kaldi
import glob
from tqdm import tqdm
import shutil
from modules.commons import recursive_munch, build_model, load_checkpoint
from optimizers import build_optimizer
from data.ft_dataset import build_ft_dataloader
from hf_utils import load_custom_model_from_hf
class Trainer:
def __init__(self,
config_path,
pretrained_ckpt_path,
data_dir,
run_name,
batch_size=0,
num_workers=0,
steps=1000,
save_interval=500,
max_epochs=1000,
device="cuda:0",
):
self.device = device
config = yaml.safe_load(open(config_path))
self.log_dir = os.path.join(config['log_dir'], run_name)
os.makedirs(self.log_dir, exist_ok=True)
# copy config file to log dir
shutil.copyfile(config_path, os.path.join(self.log_dir, os.path.basename(config_path)))
batch_size = config.get('batch_size', 10) if batch_size == 0 else batch_size
self.max_steps = steps
self.n_epochs = max_epochs
self.log_interval = config.get('log_interval', 10)
self.save_interval = save_interval
self.sr = config['preprocess_params'].get('sr', 22050)
self.hop_length = config['preprocess_params']['spect_params'].get('hop_length', 256)
self.win_length = config['preprocess_params']['spect_params'].get('win_length', 1024)
self.n_fft = config['preprocess_params']['spect_params'].get('n_fft', 1024)
preprocess_params = config['preprocess_params']
self.train_dataloader = build_ft_dataloader(
data_dir,
preprocess_params['spect_params'],
self.sr,
batch_size=batch_size,
num_workers=num_workers,
)
self.f0_condition = config['model_params']['DiT'].get('f0_condition', False)
self.build_sv_model(device, config)
self.build_semantic_fn(device, config)
if self.f0_condition:
self.build_f0_fn(device, config)
self.build_converter(device, config)
self.build_vocoder(device, config)
scheduler_params = {
"warmup_steps": 0,
"base_lr": 0.00001,
}
self.model_params = recursive_munch(config['model_params'])
self.model = build_model(self.model_params, stage='DiT')
_ = [self.model[key].to(device) for key in self.model]
self.model.cfm.estimator.setup_caches(max_batch_size=batch_size, max_seq_length=8192)
# initialize optimizers after preparing models for compatibility with FSDP
self.optimizer = build_optimizer({key: self.model[key] for key in self.model},
lr=float(scheduler_params['base_lr']))
if pretrained_ckpt_path is None:
# find latest checkpoint
available_checkpoints = glob.glob(os.path.join(self.log_dir, "DiT_epoch_*_step_*.pth"))
if len(available_checkpoints) > 0:
latest_checkpoint = max(
available_checkpoints, key=lambda x: int(x.split("_")[-1].split(".")[0])
)
earliest_checkpoint = min(
available_checkpoints, key=lambda x: int(x.split("_")[-1].split(".")[0])
)
# delete the earliest checkpoint if we have more than 2
if (
earliest_checkpoint != latest_checkpoint
and len(available_checkpoints) > 2
):
os.remove(earliest_checkpoint)
print(f"Removed {earliest_checkpoint}")
elif config.get('pretrained_model', ''):
latest_checkpoint = load_custom_model_from_hf("Plachta/Seed-VC", config['pretrained_model'], None)
else:
latest_checkpoint = ""
else:
assert os.path.exists(pretrained_ckpt_path), f"Pretrained checkpoint {pretrained_ckpt_path} not found"
latest_checkpoint = pretrained_ckpt_path
if os.path.exists(latest_checkpoint):
self.model, self.optimizer, self.epoch, self.iters = load_checkpoint(
self.model, self.optimizer, latest_checkpoint,
load_only_params=True,
ignore_modules=[],
is_distributed=False
)
print(f"Loaded checkpoint from {latest_checkpoint}")
else:
self.epoch, self.iters = 0, 0
print("Failed to load any checkpoint, training from scratch.")
def build_sv_model(self, device, config):
from modules.campplus.DTDNN import CAMPPlus
self.campplus_model = CAMPPlus(feat_dim=80, embedding_size=192)
campplus_sd_path = load_custom_model_from_hf("funasr/campplus", "campplus_cn_common.bin", config_filename=None)
campplus_sd = torch.load(campplus_sd_path, map_location='cpu')
self.campplus_model.load_state_dict(campplus_sd)
self.campplus_model.eval()
self.campplus_model.to(device)
self.sv_fn = self.campplus_model
def build_f0_fn(self, device, config):
from modules.rmvpe import RMVPE
model_path = load_custom_model_from_hf("lj1995/VoiceConversionWebUI", "rmvpe.pt", None)
self.rmvpe = RMVPE(model_path, is_half=False, device=device)
self.f0_fn = self.rmvpe
def build_converter(self, device, config):
from modules.openvoice.api import ToneColorConverter
ckpt_converter, config_converter = load_custom_model_from_hf("myshell-ai/OpenVoiceV2", "converter/checkpoint.pth", "converter/config.json")
self.tone_color_converter = ToneColorConverter(config_converter, device=device)
self.tone_color_converter.load_ckpt(ckpt_converter)
self.tone_color_converter.model.eval()
se_db_path = load_custom_model_from_hf("Plachta/Seed-VC", "se_db.pt", None)
self.se_db = torch.load(se_db_path, map_location='cpu')
def build_vocoder(self, device, config):
vocoder_type = config['model_params']['vocoder']['type']
vocoder_name = config['model_params']['vocoder'].get('name', None)
if vocoder_type == 'bigvgan':
from modules.bigvgan import bigvgan
self.bigvgan_model = bigvgan.BigVGAN.from_pretrained(vocoder_name, use_cuda_kernel=False)
self.bigvgan_model.remove_weight_norm()
self.bigvgan_model = self.bigvgan_model.eval().to(device)
vocoder_fn = self.bigvgan_model
elif vocoder_type == 'hifigan':
from modules.hifigan.generator import HiFTGenerator
from modules.hifigan.f0_predictor import ConvRNNF0Predictor
hift_config = yaml.safe_load(open('configs/hifigan.yml', 'r'))
hift_path = load_custom_model_from_hf("FunAudioLLM/CosyVoice-300M", 'hift.pt', None)
self.hift_gen = HiFTGenerator(**hift_config['hift'],
f0_predictor=ConvRNNF0Predictor(**hift_config['f0_predictor']))
self.hift_gen.load_state_dict(torch.load(hift_path, map_location='cpu'))
self.hift_gen.eval()
self.hift_gen.to(device)
vocoder_fn = self.hift_gen
else:
raise ValueError(f"Unsupported vocoder type: {vocoder_type}")
self.vocoder_fn = vocoder_fn
def build_semantic_fn(self, device, config):
speech_tokenizer_type = config['model_params']['speech_tokenizer'].get('type', 'cosyvoice')
if speech_tokenizer_type == 'whisper':
from transformers import AutoFeatureExtractor, WhisperModel
whisper_model_name = config['model_params']['speech_tokenizer']['name']
self.whisper_model = WhisperModel.from_pretrained(whisper_model_name).to(device)
self.whisper_feature_extractor = AutoFeatureExtractor.from_pretrained(whisper_model_name)
# remove decoder to save memory
del self.whisper_model.decoder
def semantic_fn(waves_16k):
ori_inputs = self.whisper_feature_extractor(
[w16k.cpu().numpy() for w16k in waves_16k],
return_tensors="pt",
return_attention_mask=True,
sampling_rate=16000,
)
ori_input_features = self.whisper_model._mask_input_features(
ori_inputs.input_features, attention_mask=ori_inputs.attention_mask
).to(device)
with torch.no_grad():
ori_outputs = self.whisper_model.encoder(
ori_input_features.to(self.whisper_model.encoder.dtype),
head_mask=None,
output_attentions=False,
output_hidden_states=False,
return_dict=True,
)
S_ori = ori_outputs.last_hidden_state.to(torch.float32)
S_ori = S_ori[:, :waves_16k.size(-1) // 320 + 1]
return S_ori
elif speech_tokenizer_type == 'xlsr':
from transformers import (
Wav2Vec2FeatureExtractor,
Wav2Vec2Model,
)
model_name = config['model_params']['speech_tokenizer']['name']
output_layer = config['model_params']['speech_tokenizer']['output_layer']
self.wav2vec_feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(model_name)
self.wav2vec_model = Wav2Vec2Model.from_pretrained(model_name)
self.wav2vec_model.encoder.layers = self.wav2vec_model.encoder.layers[:output_layer]
self.wav2vec_model = self.wav2vec_model.to(device)
self.wav2vec_model = self.wav2vec_model.eval()
self.wav2vec_model = self.wav2vec_model.half()
def semantic_fn(waves_16k):
ori_waves_16k_input_list = [waves_16k[bib].cpu().numpy() for bib in range(len(waves_16k))]
ori_inputs = self.wav2vec_feature_extractor(
ori_waves_16k_input_list,
return_tensors="pt",
return_attention_mask=True,
padding=True,
sampling_rate=16000
).to(device)
with torch.no_grad():
ori_outputs = self.wav2vec_model(
ori_inputs.input_values.half(),
)
S_ori = ori_outputs.last_hidden_state.float()
return S_ori
else:
raise ValueError(f"Unsupported speech tokenizer type: {speech_tokenizer_type}")
self.semantic_fn = semantic_fn
def train_one_step(self, batch):
waves, mels, wave_lengths, mel_input_length = batch
B = waves.size(0)
target_size = mels.size(2)
target = mels
target_lengths = mel_input_length
# get speaker embedding
if self.sr != 22050:
waves_22k = torchaudio.functional.resample(waves, self.sr, 22050)
wave_lengths_22k = (wave_lengths.float() * 22050 / self.sr).long()
else:
waves_22k = waves
wave_lengths_22k = wave_lengths
se_batch = self.tone_color_converter.extract_se(waves_22k, wave_lengths_22k)
ref_se_idx = torch.randint(0, len(self.se_db), (B,))
ref_se = self.se_db[ref_se_idx].to(self.device)
# convert
converted_waves_22k = self.tone_color_converter.convert(
waves_22k, wave_lengths_22k, se_batch, ref_se
).squeeze(1)
if self.sr != 22050:
converted_waves = torchaudio.functional.resample(converted_waves_22k, 22050, self.sr)
else:
converted_waves = converted_waves_22k
waves_16k = torchaudio.functional.resample(waves, self.sr, 16000)
wave_lengths_16k = (wave_lengths.float() * 16000 / self.sr).long()
converted_waves_16k = torchaudio.functional.resample(converted_waves, self.sr, 16000)
# extract S_alt (perturbed speech tokens)
S_ori = self.semantic_fn(waves_16k)
S_alt = self.semantic_fn(converted_waves_16k)
if self.f0_condition:
F0_ori = self.rmvpe.infer_from_audio_batch(waves_16k)
else:
F0_ori = None
# interpolate speech token to match acoustic feature length
alt_cond, _, alt_codes, alt_commitment_loss, alt_codebook_loss = (
self.model.length_regulator(S_alt, ylens=target_lengths, f0=F0_ori)
)
ori_cond, _, ori_codes, ori_commitment_loss, ori_codebook_loss = (
self.model.length_regulator(S_ori, ylens=target_lengths, f0=F0_ori)
)
if alt_commitment_loss is None:
alt_commitment_loss = 0
alt_codebook_loss = 0
ori_commitment_loss = 0
ori_codebook_loss = 0
# randomly set a length as prompt
prompt_len_max = target_lengths - 1
prompt_len = (torch.rand([B], device=alt_cond.device) * prompt_len_max).floor().long()
prompt_len[torch.rand([B], device=alt_cond.device) < 0.1] = 0
# for prompt cond token, use ori_cond instead of alt_cond
cond = alt_cond.clone()
for bib in range(B):
cond[bib, :prompt_len[bib]] = ori_cond[bib, :prompt_len[bib]]
# diffusion target
common_min_len = min(target_size, cond.size(1))
target = target[:, :, :common_min_len]
cond = cond[:, :common_min_len]
target_lengths = torch.clamp(target_lengths, max=common_min_len)
x = target
# style vectors are extracted from the prompt only
feat_list = []
for bib in range(B):
feat = kaldi.fbank(
waves_16k[bib:bib + 1, :wave_lengths_16k[bib]],
num_mel_bins=80,
dither=0,
sample_frequency=16000
)
feat = feat - feat.mean(dim=0, keepdim=True)
feat_list.append(feat)
y_list = []
with torch.no_grad():
for feat in feat_list:
y = self.sv_fn(feat.unsqueeze(0))
y_list.append(y)
y = torch.cat(y_list, dim=0)
loss, _ = self.model.cfm(x, target_lengths, prompt_len, cond, y)
loss_total = (
loss +
(alt_commitment_loss + ori_commitment_loss) * 0.05 +
(ori_codebook_loss + alt_codebook_loss) * 0.15
)
self.optimizer.zero_grad()
loss_total.backward()
torch.nn.utils.clip_grad_norm_(self.model.cfm.parameters(), 10.0)
torch.nn.utils.clip_grad_norm_(self.model.length_regulator.parameters(), 10.0)
self.optimizer.step('cfm')
self.optimizer.step('length_regulator')
self.optimizer.scheduler(key='cfm')
self.optimizer.scheduler(key='length_regulator')
return loss.detach().item()
def train_one_epoch(self):
_ = [self.model[key].train() for key in self.model]
for i, batch in enumerate(tqdm(self.train_dataloader)):
batch = [b.to(self.device) for b in batch]
loss = self.train_one_step(batch)
self.ema_loss = (
self.ema_loss * self.loss_smoothing_rate + loss * (1 - self.loss_smoothing_rate)
if self.iters > 0 else loss
)
if self.iters % self.log_interval == 0:
print(f"epoch {self.epoch}, step {self.iters}, loss: {self.ema_loss}")
self.iters += 1
if self.iters >= self.max_steps:
break
if self.iters % self.save_interval == 0:
print('Saving..')
state = {
'net': {key: self.model[key].state_dict() for key in self.model},
'optimizer': self.optimizer.state_dict(),
'scheduler': self.optimizer.scheduler_state_dict(),
'iters': self.iters,
'epoch': self.epoch,
}
save_path = os.path.join(
self.log_dir,
f'DiT_epoch_{self.epoch:05d}_step_{self.iters:05d}.pth'
)
torch.save(state, save_path)
# find all checkpoints and remove old ones
checkpoints = glob.glob(os.path.join(self.log_dir, 'DiT_epoch_*.pth'))
if len(checkpoints) > 2:
checkpoints.sort(key=lambda x: int(x.split('_')[-1].split('.')[0]))
for cp in checkpoints[:-2]:
os.remove(cp)
def train(self):
self.ema_loss = 0
self.loss_smoothing_rate = 0.99
for epoch in range(self.n_epochs):
self.epoch = epoch
self.train_one_epoch()
# Save after each epoch
print('Epoch completed. Saving..')
state = {
'net': {key: self.model[key].state_dict() for key in self.model},
'optimizer': self.optimizer.state_dict(),
'scheduler': self.optimizer.scheduler_state_dict(),
'iters': self.iters,
'epoch': self.epoch,
}
save_path = os.path.join(
self.log_dir,
f'DiT_epoch_{self.epoch:05d}_step_{self.iters:05d}.pth'
)
torch.save(state, save_path)
print(f"Checkpoint saved at {save_path}")
if self.iters >= self.max_steps:
break
print('Saving final model..')
state = {
'net': {key: self.model[key].state_dict() for key in self.model},
}
os.makedirs(self.log_dir, exist_ok=True)
save_path = os.path.join(self.log_dir, 'ft_model.pth')
torch.save(state, save_path)
print(f"Final model saved at {save_path}")
def main(args):
trainer = Trainer(
config_path=args.config,
pretrained_ckpt_path=args.pretrained_ckpt,
data_dir=args.dataset_dir,
run_name=args.run_name,
batch_size=args.batch_size,
steps=args.max_steps,
max_epochs=args.max_epochs,
save_interval=args.save_every,
num_workers=args.num_workers,
device=args.device
)
trainer.train()
if __name__ == '__main__':
if sys.platform == 'win32':
mp.freeze_support()
mp.set_start_method('spawn', force=True)
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, default='./configs/presets/config_dit_mel_seed_uvit_xlsr_tiny.yml')
parser.add_argument('--pretrained-ckpt', type=str, default=None)
parser.add_argument('--dataset-dir', type=str, default='/path/to/dataset')
parser.add_argument('--run-name', type=str, default='my_run')
parser.add_argument('--batch-size', type=int, default=2)
parser.add_argument('--max-steps', type=int, default=1000)
parser.add_argument('--max-epochs', type=int, default=1000)
parser.add_argument('--save-every', type=int, default=500)
parser.add_argument('--num-workers', type=int, default=0)
parser.add_argument("--gpu", type=int, help="Which GPU id to use", default=0)
args = parser.parse_args()
args.device = f"cuda:{args.gpu}" if args.gpu else "cuda:0"
main(args)