File size: 10,912 Bytes
39d2f14
 
597cecf
 
 
39d2f14
 
 
 
597cecf
39d2f14
 
597cecf
 
39d2f14
 
 
597cecf
39d2f14
597cecf
 
 
 
 
 
 
 
 
 
 
 
39d2f14
 
 
 
 
 
 
 
 
597cecf
 
 
 
 
 
 
 
 
 
 
39d2f14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597cecf
 
39d2f14
 
 
597cecf
 
 
 
 
 
 
 
 
 
39d2f14
597cecf
 
 
39d2f14
 
 
 
 
 
597cecf
 
39d2f14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597cecf
 
 
39d2f14
 
597cecf
 
 
 
 
 
 
 
 
 
 
39d2f14
597cecf
 
 
 
 
 
 
 
 
39d2f14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597cecf
 
 
39d2f14
 
 
 
 
 
 
 
597cecf
 
 
39d2f14
597cecf
 
 
 
 
 
39d2f14
 
 
 
 
597cecf
 
 
39d2f14
 
 
 
 
 
 
 
 
597cecf
39d2f14
 
 
 
 
597cecf
 
 
 
 
 
39d2f14
597cecf
 
 
 
 
 
 
 
 
39d2f14
 
 
 
 
597cecf
 
39d2f14
 
 
 
 
 
597cecf
39d2f14
597cecf
39d2f14
 
 
 
597cecf
39d2f14
 
597cecf
39d2f14
 
 
 
 
 
 
597cecf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39d2f14
 
 
 
597cecf
 
 
 
 
39d2f14
 
 
 
 
 
 
 
 
 
597cecf
39d2f14
597cecf
39d2f14
 
 
 
 
597cecf
39d2f14
597cecf
 
 
 
 
 
39d2f14
 
 
597cecf
39d2f14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597cecf
39d2f14
 
597cecf
39d2f14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
597cecf
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
# part of the code is borrowed from https://github.com/lawlict/ECAPA-TDNN

# from ctcmodel_nopool import ConformerCTC as ConformerCTCNoPool
from pathlib import Path

import torch
import torch.nn as nn
import torch.nn.functional as F
import torchaudio.transforms as trans

from ctcmodel import ConformerCTC

""" Res2Conv1d + BatchNorm1d + ReLU
"""


class Res2Conv1dReluBn(nn.Module):
    """
    in_channels == out_channels == channels
    """

    def __init__(
        self,
        channels,
        kernel_size=1,
        stride=1,
        padding=0,
        dilation=1,
        bias=True,
        scale=4,
    ):
        super().__init__()
        assert channels % scale == 0, "{} % {} != 0".format(channels, scale)
        self.scale = scale
        self.width = channels // scale
        self.nums = scale if scale == 1 else scale - 1

        self.convs = []
        self.bns = []
        for i in range(self.nums):
            self.convs.append(
                nn.Conv1d(
                    self.width,
                    self.width,
                    kernel_size,
                    stride,
                    padding,
                    dilation,
                    bias=bias,
                )
            )
            self.bns.append(nn.BatchNorm1d(self.width))
        self.convs = nn.ModuleList(self.convs)
        self.bns = nn.ModuleList(self.bns)

    def forward(self, x):
        out = []
        spx = torch.split(x, self.width, 1)
        for i in range(self.nums):
            if i == 0:
                sp = spx[i]
            else:
                sp = sp + spx[i]
            # Order: conv -> relu -> bn
            sp = self.convs[i](sp)
            sp = self.bns[i](F.relu(sp))
            out.append(sp)
        if self.scale != 1:
            out.append(spx[self.nums])
        out = torch.cat(out, dim=1)

        return out


""" Conv1d + BatchNorm1d + ReLU
"""


class Conv1dReluBn(nn.Module):
    def __init__(
        self,
        in_channels,
        out_channels,
        kernel_size=1,
        stride=1,
        padding=0,
        dilation=1,
        bias=True,
    ):
        super().__init__()
        self.conv = nn.Conv1d(
            in_channels, out_channels, kernel_size, stride, padding, dilation, bias=bias
        )
        self.bn = nn.BatchNorm1d(out_channels)

    def forward(self, x):
        return self.bn(F.relu(self.conv(x)))


""" The SE connection of 1D case.
"""


class SE_Connect(nn.Module):
    def __init__(self, channels, se_bottleneck_dim=128):
        super().__init__()
        self.linear1 = nn.Linear(channels, se_bottleneck_dim)
        self.linear2 = nn.Linear(se_bottleneck_dim, channels)

    def forward(self, x):
        out = x.mean(dim=2)
        out = F.relu(self.linear1(out))
        out = torch.sigmoid(self.linear2(out))
        out = x * out.unsqueeze(2)

        return out


""" SE-Res2Block of the ECAPA-TDNN architecture.
"""


class SE_Res2Block(nn.Module):
    def __init__(
        self,
        in_channels,
        out_channels,
        kernel_size,
        stride,
        padding,
        dilation,
        scale,
        se_bottleneck_dim,
    ):
        super().__init__()
        self.Conv1dReluBn1 = Conv1dReluBn(
            in_channels, out_channels, kernel_size=1, stride=1, padding=0
        )
        self.Res2Conv1dReluBn = Res2Conv1dReluBn(
            out_channels, kernel_size, stride, padding, dilation, scale=scale
        )
        self.Conv1dReluBn2 = Conv1dReluBn(
            out_channels, out_channels, kernel_size=1, stride=1, padding=0
        )
        self.SE_Connect = SE_Connect(out_channels, se_bottleneck_dim)

        self.shortcut = None
        if in_channels != out_channels:
            self.shortcut = nn.Conv1d(
                in_channels=in_channels,
                out_channels=out_channels,
                kernel_size=1,
            )

    def forward(self, x):
        residual = x
        if self.shortcut:
            residual = self.shortcut(x)

        x = self.Conv1dReluBn1(x)
        x = self.Res2Conv1dReluBn(x)
        x = self.Conv1dReluBn2(x)
        x = self.SE_Connect(x)

        return x + residual


""" Attentive weighted mean and standard deviation pooling.
"""


class AttentiveStatsPool(nn.Module):
    def __init__(self, in_dim, attention_channels=128, global_context_att=False):
        super().__init__()
        self.global_context_att = global_context_att

        # Use Conv1d with stride == 1 rather than Linear, then we don't need to transpose inputs.
        if global_context_att:
            self.linear1 = nn.Conv1d(
                in_dim * 3, attention_channels, kernel_size=1
            )  # equals W and b in the paper
        else:
            self.linear1 = nn.Conv1d(
                in_dim, attention_channels, kernel_size=1
            )  # equals W and b in the paper
        self.linear2 = nn.Conv1d(
            attention_channels, in_dim, kernel_size=1
        )  # equals V and k in the paper

    def forward(self, x):

        if self.global_context_att:
            context_mean = torch.mean(x, dim=-1, keepdim=True).expand_as(x)
            context_std = torch.sqrt(
                torch.var(x, dim=-1, keepdim=True) + 1e-10
            ).expand_as(x)
            x_in = torch.cat((x, context_mean, context_std), dim=1)
        else:
            x_in = x

        # DON'T use ReLU here! In experiments, I find ReLU hard to converge.
        alpha = torch.tanh(self.linear1(x_in))
        # alpha = F.relu(self.linear1(x_in))
        alpha = torch.softmax(self.linear2(alpha), dim=2)
        mean = torch.sum(alpha * x, dim=2)
        residuals = torch.sum(alpha * (x**2), dim=2) - mean**2
        std = torch.sqrt(residuals.clamp(min=1e-9))
        return torch.cat([mean, std], dim=1)


class ECAPA_TDNN(nn.Module):
    def __init__(
        self,
        channels=512,
        emb_dim=512,
        global_context_att=False,
        use_fp16=True,
        ctc_cls=ConformerCTC,
        ctc_path="/data4/F5TTS/ckpts/F5TTS_norm_ASR_vocos_pinyin_Emilia_ZH_EN/model_last.pt",
        ctc_args={
            "vocab_size": 2545,
            "mel_dim": 100,
            "num_heads": 8,
            "d_hid": 512,
            "nlayers": 6,
        },
        ctc_no_grad=False,
    ):
        super().__init__()
        if ctc_path != None:
            ctc_path = Path(ctc_path)
            model = ctc_cls(**ctc_args)
            state_dict = torch.load(ctc_path, map_location="cpu")
            model.load_state_dict(state_dict["model_state_dict"])
            print(f"Initialized pretrained ConformerCTC backbone from {ctc_path}.")
        else:
            raise ValueError(ctc_path)

        self.ctc_model = model
        self.ctc_model.out.requires_grad_(False)

        if ctc_cls == ConformerCTC:
            self.feat_num = ctc_args["nlayers"] + 2 + 1
        # elif ctc_cls == ConformerCTCNoPool:
        #     self.feat_num = ctc_args['nlayers'] + 1
        else:
            raise ValueError(ctc_cls)
        feat_dim = ctc_args["d_hid"]

        self.emb_dim = emb_dim

        self.feature_weight = nn.Parameter(torch.zeros(self.feat_num))
        self.instance_norm = nn.InstanceNorm1d(feat_dim)

        # self.channels = [channels] * 4 + [channels * 3]
        self.channels = [channels] * 4 + [1536]

        self.layer1 = Conv1dReluBn(feat_dim, self.channels[0], kernel_size=5, padding=2)
        self.layer2 = SE_Res2Block(
            self.channels[0],
            self.channels[1],
            kernel_size=3,
            stride=1,
            padding=2,
            dilation=2,
            scale=8,
            se_bottleneck_dim=128,
        )
        self.layer3 = SE_Res2Block(
            self.channels[1],
            self.channels[2],
            kernel_size=3,
            stride=1,
            padding=3,
            dilation=3,
            scale=8,
            se_bottleneck_dim=128,
        )
        self.layer4 = SE_Res2Block(
            self.channels[2],
            self.channels[3],
            kernel_size=3,
            stride=1,
            padding=4,
            dilation=4,
            scale=8,
            se_bottleneck_dim=128,
        )

        # self.conv = nn.Conv1d(self.channels[-1], self.channels[-1], kernel_size=1)
        cat_channels = channels * 3
        self.conv = nn.Conv1d(cat_channels, self.channels[-1], kernel_size=1)
        self.pooling = AttentiveStatsPool(
            self.channels[-1],
            attention_channels=128,
            global_context_att=global_context_att,
        )
        self.bn = nn.BatchNorm1d(self.channels[-1] * 2)
        self.linear = nn.Linear(self.channels[-1] * 2, emb_dim)

        if ctc_no_grad:
            for param in self.ctc_model.parameters():
                param.requires_grad = False
            self.ctc_model = self.ctc_model.eval()
        else:
            self.ctc_model = self.ctc_model.train()
        self.ctc_no_grad = ctc_no_grad
        print("ctc_no_grad: ", self.ctc_no_grad)

    def forward(self, latent, input_lengths, return_asr=False):
        if self.ctc_no_grad:
            with torch.no_grad():
                asr, h = self.ctc_model(latent, input_lengths)
        else:
            asr, h = self.ctc_model(latent, input_lengths)

        x = torch.stack(h, dim=0)
        norm_weights = (
            F.softmax(self.feature_weight, dim=-1)
            .unsqueeze(-1)
            .unsqueeze(-1)
            .unsqueeze(-1)
        )
        x = (norm_weights * x).sum(dim=0)
        x = x + 1e-6
        # x = torch.transpose(x, 1, 2) + 1e-6

        x = self.instance_norm(x)
        # x = torch.transpose(x, 1, 2)

        out1 = self.layer1(x)
        out2 = self.layer2(out1)
        out3 = self.layer3(out2)
        out4 = self.layer4(out3)

        out = torch.cat([out2, out3, out4], dim=1)
        out = F.relu(self.conv(out))
        out = self.bn(self.pooling(out))
        out = self.linear(out)

        if return_asr:
            return out, asr
        return out


if __name__ == "__main__":
    from diffspeech.data.collate import get_mask_from_lengths
    from diffspeech.ldm.model import DiT
    from diffspeech.tools.text.vocab import IPA

    bsz = 3

    # Sample ipa
    ipa_lens = torch.randint(10, 50, (bsz,)).cuda()
    ipa_mask = get_mask_from_lengths(ipa_lens).cuda()
    ipa = torch.randint(0, len(IPA.vocab), (bsz, ipa_mask.size(-1))).cuda()

    # Sample latent
    latent_lens = torch.randint(50, 250, (bsz,)).cuda()
    latent_mask = get_mask_from_lengths(latent_lens).cuda()
    latent = torch.randn(bsz, latent_mask.size(-1), 64).cuda()

    # Sample prompt
    prompt_mask = get_mask_from_lengths(
        (latent_lens * 0.25).long(), max_len=latent_mask.size(-1)
    ).cuda()
    prompt_latent = latent * prompt_mask.unsqueeze(-1)

    model = ECAPA_TDNN(emb_dim=512).cuda()

    emb = model(latent, latent_mask.sum(axis=-1))

    print(emb.shape)