File size: 5,540 Bytes
7375975
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import hydra
from hydra import compose, initialize
from hydra.utils import instantiate
from lightning import LightningModule
from loguru import logger
from omegaconf import OmegaConf
import sys
sys.path.insert(0,'/workspace/user_code/kuachen/projects/v2s')

from fish_speech.models.v2s_tts.pretrain_model import V2S_TTS_Pretrain_Model
from fish_speech.models.v2s_tts.flow_matching_dit import ConditionalCFM
from fish_speech.models.v2s_tts.model.backbones.dit import DiT_Style
from fish_speech.models.v2s_tts.transformer.encoder import ConformerEncoder
from fish_speech.models.v2s_tts.style_bank import StyleBankExtractor
from omegaconf import DictConfig

class CFMParams:
    def __init__(self):
        self.sigma_min = 1e-06
        self.solver = "euler"
        self.t_scheduler = "cosine"
        self.training_cfg_rate = 0.2
        self.inference_cfg_rate = 0.7
        self.reg_loss_type = "l1"


def load_model(config_name, checkpoint_path, device="cpu"):
    hydra.core.global_hydra.GlobalHydra.instance().clear()
    with initialize(version_base="1.3", config_path="../fish_speech/configs"):
        cfg = compose(config_name=config_name)

    model: LightningModule = instantiate(cfg.model)
    state_dict = torch.load(
        checkpoint_path,
        map_location=model.device,
    )

    if "state_dict" in state_dict:
        state_dict = state_dict["state_dict"]

    model.load_state_dict(state_dict, strict=False)
    model.eval()
    model.to(device)
    logger.info("Restored model from checkpoint")

    return model

def get_pretrain_model(checkpoint_path):
    # εˆε§‹εŒ– encoder
    encoder = ConformerEncoder(
        output_size=512,
        attention_heads=8,
        linear_units=2048,
        num_blocks=6,
        dropout_rate=0.1,
        positional_dropout_rate=0.1,
        attention_dropout_rate=0.1,
        normalize_before=True,
        input_layer='linear',
        pos_enc_layer_type='rel_pos_espnet',
        selfattention_layer_type='rel_selfattn',
        input_size=512,
        use_cnn_module=False,
        macaron_style=False
    )

    # εˆε§‹εŒ– style_qformer
    style_qformer = StyleBankExtractor(
        dim_in=1024,
        n_layers=4,
        n_emb=32,
        d_model=64,
        nhead=4
    )

    # εˆε§‹εŒ– estimator
    estimator = DiT_Style(
        dim=1024,
        depth=22,
        heads=16,
        ff_mult=2,
        conv_layers=4,
        mel_dim=80,
        style_dim=64
    )

    cfm_params = CFMParams()

    # εˆε§‹εŒ– decoder
    decoder = ConditionalCFM(
        in_channels=160,
        n_spks=0,
        spk_emb_dim=80,
        cfm_params=cfm_params,
        estimator=estimator
    )

    # εˆε§‹εŒ–ζ¨‘εž‹
    model = V2S_TTS_Pretrain_Model(
        input_size=512,
        output_size=80,
        output_type='mel',
        vocab_size=500,
        spk_dim=192,
        sll_checkpoint='checkpoints/wavlm_large.pt',
        output_layer=6,
        decoder=decoder,
        encoder=encoder,
        style_qformer=style_qformer
    )
    state_dict = torch.load(checkpoint_path)['state_dict']
    new_params = {}
    for k,v in state_dict.items():
        if k.startswith('generator'):
            new_k = k[10:]
            new_params[new_k] = v
    # Load the new parameters into the model
    model.load_state_dict(new_params, strict=True)
    model.eval()
    return model


def get_pretrain_model_32_dim32(checkpoint_path):
    # εˆε§‹εŒ– encoder
    encoder = ConformerEncoder(
        output_size=512,
        attention_heads=8,
        linear_units=2048,
        num_blocks=6,
        dropout_rate=0.1,
        positional_dropout_rate=0.1,
        attention_dropout_rate=0.1,
        normalize_before=True,
        input_layer='linear',
        pos_enc_layer_type='rel_pos_espnet',
        selfattention_layer_type='rel_selfattn',
        input_size=512,
        use_cnn_module=False,
        macaron_style=False
    )

    # εˆε§‹εŒ– style_qformer
    style_qformer = StyleBankExtractor(
        dim_in=1024,
        n_layers=4,
        n_emb=32,
        d_model=32,
        nhead=4
    )

    # εˆε§‹εŒ– estimator
    estimator = DiT_Style(
        dim=1024,
        depth=22,
        heads=16,
        ff_mult=2,
        conv_layers=4,
        mel_dim=80,
        style_dim=32
    )

    cfm_params = CFMParams()

    # εˆε§‹εŒ– decoder
    decoder = ConditionalCFM(
        in_channels=160,
        n_spks=0,
        spk_emb_dim=80,
        cfm_params=cfm_params,
        estimator=estimator
    )

    # εˆε§‹εŒ–ζ¨‘εž‹
    model = V2S_TTS_Pretrain_Model(
        input_size=512,
        output_size=80,
        output_type='mel',
        vocab_size=500,
        spk_dim=192,
        sll_checkpoint='checkpoints/wavlm_large.pt',
        output_layer=6,
        decoder=decoder,
        encoder=encoder,
        style_qformer=style_qformer
    )
    state_dict = torch.load(checkpoint_path)['state_dict']
    new_params = {}
    for k,v in state_dict.items():
        if k.startswith('generator'):
            new_k = k[10:]
            new_params[new_k] = v
    # Load the new parameters into the model
    model.load_state_dict(new_params, strict=True)
    model.eval()
    return model

# ckpt_path = '/workspace/user_code/kuachen/projects/v2s/results/v2s_tts_pretrain_32_dim64/step_000336000.ckpt'
# config_name = 'v2s_tts_pretrain_32_dim64.yaml'
# model = load_model(config_name,ckpt_path)
# print(model.generator)
# model = get_pretrain_model(ckpt_path)

# state_dict = torch.load(ckpt_path)['state_dict']
# print(state_dict)