-
Notifications
You must be signed in to change notification settings - Fork 706
/
Copy pathmetis.py
523 lines (432 loc) · 16 KB
/
metis.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# Copyright (c) 2024 Amphion.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import os
import numpy as np
import torch
import torch.nn.functional as F
import safetensors
import librosa
from models.tts.metis.audio_tokenizer import AudioTokenizer
from models.tts.maskgct.maskgct_utils import build_t2s_model, build_s2a_model, g2p_
from models.tts.metis.metis_model import MetisStage1
from peft import LoraModel, LoraConfig
from huggingface_hub import hf_hub_download, snapshot_download
import langid
def build_metis_stage1(cfg, device, ft_type=None):
if ft_type == "l2s" or (
hasattr(cfg, "use_zero_gate_adapter") and not cfg.use_zero_gate_adapter
):
use_zero_gate_adapter = False
else:
use_zero_gate_adapter = True
metis_stage1 = MetisStage1(
cfg=cfg,
ft_type=ft_type,
ft_cond_dim=cfg.cond_dim,
use_zero_gate_adapter=use_zero_gate_adapter,
)
if hasattr(cfg, "use_lora") and cfg.use_lora:
lora_config = LoraConfig(
task_type="SEQ_2_SEQ_LM",
r=cfg.lora_r,
lora_alpha=cfg.lora_alpha,
target_modules=[
"q_proj",
"k_proj",
"v_proj",
"o_proj",
"gate_proj",
"up_proj",
"down_proj",
],
lora_dropout=0.01,
)
metis_stage1 = LoraModel(metis_stage1, lora_config, adapter_name="default")
metis_stage1.eval()
metis_stage1.to(device)
return metis_stage1
def build_metis_stage1_base(cfg, device, ft_type=None):
metis_stage1 = MetisStage1(cfg=cfg, ft_type=ft_type, ft_cond_dim=cfg.cond_dim)
metis_stage1.eval()
metis_stage1.to(device)
return metis_stage1
def merge_lora_weights(cfg, base_model, lora_weights):
"""Merge LoRA weights into the base model.
Args:
base_model: MetisStage1 model instance
lora_weights: dict of LoRA weights (from safetensors or torch.load)
Returns:
MetisStage1: Model with merged weights
"""
if isinstance(base_model, LoraModel):
base_model = base_model.model # Get the underlying model if it's wrapped
# Create temporary LoRA model
lora_config = LoraConfig(
task_type="SEQ_2_SEQ_LM",
r=cfg.lora_r,
lora_alpha=cfg.lora_alpha,
target_modules=[
"q_proj",
"k_proj",
"v_proj",
"o_proj",
"gate_proj",
"up_proj",
"down_proj",
],
lora_dropout=0.01,
)
temp_model = LoraModel(base_model, lora_config, adapter_name="default")
# Load LoRA weights
temp_model.load_state_dict(lora_weights, strict=False)
# Merge weights
merged_model = temp_model.merge_and_unload()
return merged_model
def merge_adapter_weights(base_model, adapter_weights, device):
"""Merge adapter weights into the base model.
Args:
base_model: MetisStage1 model instance
adapter_weights: dict of adapter weights (adapter name: cond_emb)
"""
for name, param in base_model.named_parameters():
if "cond_adapter" in name:
# print(name)
param.data = adapter_weights["model." + name]
# to device
param.data = param.data.to(device)
return base_model
def extract_lora_weights(model):
"""Extract LoRA weights from a LoraModel.
Args:
model: A LoraModel instance
Returns:
dict: LoRA weights state dict
"""
if not isinstance(model, LoraModel):
raise ValueError("Model must be a LoraModel instance")
lora_state_dict = {}
for name, param in model.named_parameters():
if "lora_" in name: # Only save LoRA parameters
lora_state_dict[name] = param.data.clone()
return lora_state_dict
def extract_adapter_weights(model):
"""Extract adapter weights from a model
Args:
model: A model instance
Returns:
dict: adapter weights state dict (adapter name: cond_emb)
"""
adapter_state_dict = {}
for name, param in model.named_parameters():
if "cond_adapter" in name:
adapter_state_dict[name] = param.data.clone()
return adapter_state_dict
def bulid_visual_encoder(cfg, device):
from models.tts.metis.vis_encoder import InferencePipeline
visual_encoder = InferencePipeline(
"video", cfg.vis_model_path, cfg.vis_model_conf, face_track=True, device=device
)
return visual_encoder
class Metis:
def __init__(
self,
ckpt_path=None,
base_ckpt_path=None,
lora_ckpt_path=None,
adapter_ckpt_path=None,
cfg=None,
device="cuda",
model_type=None, # support ["tts", "vc", "se", "tse", "l2s", "mix"]
):
self.ckpt_path = ckpt_path
self.cfg = cfg
self.device = device
self.model_type = model_type
self.audio_tokenizer = AudioTokenizer(cfg, device)
self.s2a_model_1layer, self.s2a_model_full = self._build_s2a_model()
if ckpt_path is not None:
self.metis_stage1 = build_metis_stage1(
cfg.model.t2s_model, device, model_type
)
safetensors.torch.load_model(self.metis_stage1, ckpt_path)
if model_type == "l2s":
self.visual_encoder = bulid_visual_encoder(cfg.model.t2s_model, device)
else:
self.metis_stage1 = build_metis_stage1_base(
cfg.model.t2s_model, device, ft_type=model_type
)
safetensors.torch.load_model(
self.metis_stage1, base_ckpt_path, strict=False
)
print("load base model")
# load adapter weights
adapter_weights = safetensors.torch.load_file(adapter_ckpt_path)
self.metis_stage1 = merge_adapter_weights(
self.metis_stage1, adapter_weights, device
)
print("load adapter weights")
# load lora weights
lora_weights = safetensors.torch.load_file(lora_ckpt_path)
self.metis_stage1 = merge_lora_weights(
cfg.model.t2s_model, self.metis_stage1, lora_weights
)
print("load lora weights")
@torch.no_grad()
def __call__(
self,
text: str = None,
prompt_speech_path: str = None,
source_speech_path: str = None, # used for se, tse, vc
source_video_path: str = None, # used for l2s
prompt_text: str = None,
prompt_language: str = None,
target_language: str = None,
target_len=None, # in seconds
n_timesteps: int = 25,
cfg: float = 2.5,
halton_scheduler: bool = False,
model_type: str = "tts",
):
if prompt_speech_path is not None:
prompt_semantic_code, prompt_acoustic_code = self.preprocess_prompt_wav(
prompt_speech_path
)
else:
prompt_semantic_code, prompt_acoustic_code = None, None
if model_type == "tts":
combine_semantic_code, prompt_semantic_code = self.text2semantic(
text,
prompt_text,
prompt_language,
target_language,
prompt_semantic_code,
target_len,
n_timesteps,
cfg,
halton_scheduler,
)
elif model_type == "se":
source_speech_16k = librosa.load(source_speech_path, sr=16000)[0]
combine_semantic_code = self.speech2semantic_wo_prompt(
source_speech_16k, n_timesteps, cfg=cfg
)
elif model_type in ["vc", "tse"]:
source_speech_16k = librosa.load(source_speech_path, sr=16000)[0]
prompt_speech_16k = librosa.load(prompt_speech_path, sr=16000)[0]
combine_semantic_code = self.speech2semantic_w_prompt(
source_speech_16k,
prompt_speech_16k,
prompt_semantic_code,
n_timesteps,
cfg=cfg,
)
elif model_type == "l2s":
video_feature = self.visual_encoder.extract_features(source_video_path)
combine_semantic_code = self.video2semantic(
prompt_semantic_code, video_feature, n_timesteps, cfg
)
predict_acoustic_code = self.semantic2acoustic(
combine_semantic_code, prompt_acoustic_code
)
predict_wav = self.audio_tokenizer.code2wav(predict_acoustic_code)
return predict_wav
def _build_s2a_model(self):
# build s2a model
s2a_model_1layer = build_s2a_model(
self.cfg.model.s2a_model.s2a_1layer, self.device
)
s2a_model_full = build_s2a_model(self.cfg.model.s2a_model.s2a_full, self.device)
# download s2a model
s2a_1layer_dir = snapshot_download(
repo_id="amphion/MaskGCT",
repo_type="model",
local_dir="./models/tts/metis/ckpt",
allow_patterns=["s2a_model/s2a_model_1layer/model.safetensors"],
)
s2a_full_dir = snapshot_download(
repo_id="amphion/MaskGCT",
repo_type="model",
local_dir="./models/tts/metis/ckpt",
allow_patterns=["s2a_model/s2a_model_full/model.safetensors"],
)
s2a_1layer_ckpt = os.path.join(
s2a_1layer_dir, "s2a_model/s2a_model_1layer/model.safetensors"
)
s2a_full_ckpt = os.path.join(
s2a_full_dir, "s2a_model/s2a_model_full/model.safetensors"
)
# load s2a model
safetensors.torch.load_model(s2a_model_1layer, s2a_1layer_ckpt)
safetensors.torch.load_model(s2a_model_full, s2a_full_ckpt)
s2a_model_1layer.eval()
s2a_model_full.eval()
return s2a_model_1layer, s2a_model_full
@torch.no_grad()
def preprocess_prompt_wav(self, prompt_speech_path: str):
prompt_semantic_code, _, prompt_acoustic_code = self.audio_tokenizer(
speech_path=prompt_speech_path
)
return prompt_semantic_code, prompt_acoustic_code
@torch.no_grad()
def semantic2acoustic(self, combine_semantic_code, acoustic_code):
if acoustic_code is None: # if no prompt
acoustic_code = torch.zeros(1, 0, 12).to(self.device).long()
semantic_code = combine_semantic_code
cond = self.s2a_model_1layer.cond_emb(semantic_code)
prompt = acoustic_code[:, :, :]
predict_1layer = self.s2a_model_1layer.reverse_diffusion(
cond=cond,
prompt=prompt,
temp=1.5,
filter_thres=0.98,
n_timesteps=[40],
cfg=0,
rescale_cfg=0.75,
)
cond = self.s2a_model_full.cond_emb(semantic_code)
prompt = acoustic_code[:, :, :]
predict_full = self.s2a_model_full.reverse_diffusion(
cond=cond,
prompt=prompt,
temp=1.5,
filter_thres=0.98,
n_timesteps=[40, 16, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
cfg=2.5,
rescale_cfg=0.75,
gt_code=predict_1layer,
)
return predict_full
def tokenizer(
self, text: str, prompt_text: str, prompt_language=None, target_language=None
):
if prompt_language is None:
prompt_language = langid.classify(prompt_text)[0]
if target_language is None:
target_language = langid.classify(text)[0]
if prompt_language not in ["zh", "en", "ja", "fr", "ko", "de"]:
prompt_language = "en"
if target_language not in ["zh", "en", "ja", "fr", "ko", "de"]:
target_language = "en"
prompt_phone_id = g2p_(prompt_text, prompt_language)[1]
target_phone_id = g2p_(text, target_language)[1]
prompt_phone_id = torch.tensor(prompt_phone_id, dtype=torch.long).to(
self.device
)
target_phone_id = torch.tensor(target_phone_id, dtype=torch.long).to(
self.device
)
phone_id = torch.cat([prompt_phone_id, target_phone_id])
return phone_id, prompt_phone_id, target_phone_id
@torch.no_grad()
def speech2semantic_wo_prompt(self, speech: np.ndarray, steps=10, cfg=0):
semantic_feat = self.audio_tokenizer.wav2semantic_feat(speech)
predict_semantic_code = self.metis_stage1.reverse_diffusion(
torch.zeros(1, 0).to(self.device).long(),
semantic_feat.shape[1],
None,
n_timesteps=steps,
cfg=cfg,
rescale_cfg=0.75,
finetune_cond=semantic_feat,
)
return predict_semantic_code
@torch.no_grad()
def speech2semantic_w_prompt(
self,
speech: np.ndarray,
prompt_speech: np.ndarray,
prompt_semantic_code: torch.Tensor,
steps=10,
cfg=0,
):
semantic_feat = self.audio_tokenizer.wav2semantic_feat(
np.concatenate([prompt_speech, speech])
)
target_len = semantic_feat.shape[1] - prompt_semantic_code.shape[1]
predict_semantic_code = self.metis_stage1.reverse_diffusion(
prompt_semantic_code,
target_len,
None,
n_timesteps=steps,
cfg=cfg,
rescale_cfg=0.75,
finetune_cond=semantic_feat,
)
combine_semantic_code = torch.cat(
[prompt_semantic_code, predict_semantic_code], dim=-1
)
return combine_semantic_code
@torch.no_grad()
def text2semantic(
self,
text,
prompt_text,
prompt_language,
target_language,
prompt_semantic_code,
target_len=None,
n_timesteps=25,
cfg=2.5,
halton_scheduler=False,
):
phone_id, prompt_phone_id, target_phone_id = self.tokenizer(
text, prompt_text, prompt_language, target_language
)
if target_len is None:
target_len = (
prompt_semantic_code.shape[1]
* len(text.encode("utf-8"))
// len(prompt_text.encode("utf-8"))
)
else:
target_len = int(target_len * 50) # 50 tokens per second
# TODO: halton scheduler
# if halton_scheduler:
# preschedule_mask_indices = discrete_halton_sequence(2, target_len)
# else:
# preschedule_mask_indices = None
predict_semantic = self.metis_stage1.reverse_diffusion(
prompt_semantic_code[:, :],
target_len,
phone_id.unsqueeze(0),
n_timesteps=n_timesteps,
cfg=cfg,
rescale_cfg=0.75,
# preschedule_mask_indices=preschedule_mask_indices,
)
combine_semantic_code = torch.cat(
[prompt_semantic_code[:, :], predict_semantic], dim=-1
)
return combine_semantic_code, prompt_semantic_code
@torch.no_grad()
def video2semantic(
self, prompt_semantic_code, video_feature, n_timesteps=25, cfg=0
):
video_feature = video_feature.unsqueeze(0).transpose(1, 2)
video_feature = F.interpolate(
video_feature, scale_factor=2, mode="linear", align_corners=False
)
video_feature = video_feature.transpose(1, 2).squeeze(0)
target_len = video_feature.shape[0]
prompt_len = prompt_semantic_code.shape[-1]
if prompt_len > 0:
zeros_tensor = torch.zeros((prompt_len, video_feature.shape[-1])).to(
self.device
)
video_feature = torch.cat((zeros_tensor, video_feature), dim=0)
video_feature = video_feature.unsqueeze(0)
predict_semantic_code = self.metis_stage1.reverse_diffusion(
prompt_semantic_code,
target_len,
None,
n_timesteps=n_timesteps,
cfg=cfg,
rescale_cfg=0.75,
finetune_cond=video_feature,
)
combine_semantic_code = torch.cat(
[prompt_semantic_code, predict_semantic_code], dim=-1
)
return combine_semantic_code