File size: 17,210 Bytes
bcdb559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import numpy as np
import torch.nn.functional as F

class SLMAdversarialLoss(torch.nn.Module):

    def __init__(self, model, wl, sampler, min_len, max_len, batch_percentage=0.5, skip_update=10, sig=1.5):
        super(SLMAdversarialLoss, self).__init__()
        self.model = model
        self.wl = wl
        self.sampler = sampler
        
        self.min_len = min_len
        self.max_len = max_len
        self.batch_percentage = batch_percentage
        
        self.sig = sig
        self.skip_update = skip_update
        
    def forward(self, iters, y_rec_gt, y_rec_gt_pred, waves, mel_input_length, ref_text, ref_lengths, use_ind, s_trg, ref_s=None):
        text_mask = length_to_mask(ref_lengths).to(ref_text.device)
        bert_dur = self.model.bert(ref_text, attention_mask=(~text_mask).int())
        d_en = self.model.bert_encoder(bert_dur).transpose(-1, -2) 
        
        if use_ind and np.random.rand() < 0.5:
            s_preds = s_trg
        else:
            num_steps = np.random.randint(3, 5)
            if ref_s is not None:
                s_preds = self.sampler(noise = torch.randn_like(s_trg).unsqueeze(1).to(ref_text.device), 
                      embedding=bert_dur,
                      embedding_scale=1,
                               features=ref_s, # reference from the same speaker as the embedding
                         embedding_mask_proba=0.1,
                         num_steps=num_steps).squeeze(1)
            else:
                s_preds = self.sampler(noise = torch.randn_like(s_trg).unsqueeze(1).to(ref_text.device), 
                      embedding=bert_dur,
                      embedding_scale=1,
                         embedding_mask_proba=0.1,
                         num_steps=num_steps).squeeze(1)
            
        s_dur = s_preds[:, 128:]
        s = s_preds[:, :128]
        
        d, _ = self.model.predictor(d_en, s_dur, 
                                                ref_lengths, 
                                                torch.randn(ref_lengths.shape[0], ref_lengths.max(), 2).to(ref_text.device), 
                                                text_mask)
        
        bib = 0

        output_lengths = []
        attn_preds = []
        
        # differentiable duration modeling
        for _s2s_pred, _text_length in zip(d, ref_lengths):

            _s2s_pred_org = _s2s_pred[:_text_length, :]

            _s2s_pred = torch.sigmoid(_s2s_pred_org)
            _dur_pred = _s2s_pred.sum(axis=-1)

            l = int(torch.round(_s2s_pred.sum()).item())
            t = torch.arange(0, l).expand(l)

            t = torch.arange(0, l).unsqueeze(0).expand((len(_s2s_pred), l)).to(ref_text.device)
            loc = torch.cumsum(_dur_pred, dim=0) - _dur_pred / 2

            h = torch.exp(-0.5 * torch.square(t - (l - loc.unsqueeze(-1))) / (self.sig)**2)

            out = torch.nn.functional.conv1d(_s2s_pred_org.unsqueeze(0), 
                                         h.unsqueeze(1), 
                                         padding=h.shape[-1] - 1, groups=int(_text_length))[..., :l]
            attn_preds.append(F.softmax(out.squeeze(), dim=0))

            output_lengths.append(l)

        max_len = max(output_lengths)
        
        with torch.no_grad():
            t_en = self.model.text_encoder(ref_text, ref_lengths, text_mask)
            
        s2s_attn = torch.zeros(len(ref_lengths), int(ref_lengths.max()), max_len).to(ref_text.device)
        for bib in range(len(output_lengths)):
            s2s_attn[bib, :ref_lengths[bib], :output_lengths[bib]] = attn_preds[bib]

        asr_pred = t_en @ s2s_attn

        _, p_pred = self.model.predictor(d_en, s_dur, 
                                                ref_lengths, 
                                                s2s_attn, 
                                                text_mask)
        
        mel_len = max(int(min(output_lengths) / 2 - 1), self.min_len // 2)
        mel_len = min(mel_len, self.max_len // 2)
        
        # get clips
        
        en = []
        p_en = []
        sp = []
        
        F0_fakes = []
        N_fakes = []
        
        wav = []

        for bib in range(len(output_lengths)):
            mel_length_pred = output_lengths[bib]
            mel_length_gt = int(mel_input_length[bib].item() / 2)
            if mel_length_gt <= mel_len or mel_length_pred <= mel_len:
                continue

            sp.append(s_preds[bib])

            random_start = np.random.randint(0, mel_length_pred - mel_len)
            en.append(asr_pred[bib, :, random_start:random_start+mel_len])
            p_en.append(p_pred[bib, :, random_start:random_start+mel_len])

            # get ground truth clips
            random_start = np.random.randint(0, mel_length_gt - mel_len)
            y = waves[bib][(random_start * 2) * 300:((random_start+mel_len) * 2) * 300]
            wav.append(torch.from_numpy(y).to(ref_text.device))
            
            if len(wav) >= self.batch_percentage * len(waves): # prevent OOM due to longer lengths
                break

        if len(sp) <= 1:
            return None
            
        sp = torch.stack(sp)
        wav = torch.stack(wav).float()
        en = torch.stack(en)
        p_en = torch.stack(p_en)
        
        F0_fake, N_fake = self.model.predictor.F0Ntrain(p_en, sp[:, 128:])
        y_pred = self.model.decoder(en, F0_fake, N_fake, sp[:, :128])
        
        # discriminator loss
        if (iters + 1) % self.skip_update == 0:
            if np.random.randint(0, 2) == 0:
                wav = y_rec_gt_pred
                use_rec = True
            else:
                use_rec = False

            crop_size = min(wav.size(-1), y_pred.size(-1))
            if use_rec: # use reconstructed (shorter lengths), do length invariant regularization
                if wav.size(-1) > y_pred.size(-1):
                    real_GP = wav[:, : , :crop_size]
                    out_crop = self.wl.discriminator_forward(real_GP.detach().squeeze())
                    out_org = self.wl.discriminator_forward(wav.detach().squeeze())
                    loss_reg = F.l1_loss(out_crop, out_org[..., :out_crop.size(-1)])

                    if np.random.randint(0, 2) == 0:
                        d_loss = self.wl.discriminator(real_GP.detach().squeeze(), y_pred.detach().squeeze()).mean()
                    else:
                        d_loss = self.wl.discriminator(wav.detach().squeeze(), y_pred.detach().squeeze()).mean()
                else:
                    real_GP = y_pred[:, : , :crop_size]
                    out_crop = self.wl.discriminator_forward(real_GP.detach().squeeze())
                    out_org = self.wl.discriminator_forward(y_pred.detach().squeeze())
                    loss_reg = F.l1_loss(out_crop, out_org[..., :out_crop.size(-1)])

                    if np.random.randint(0, 2) == 0:
                        d_loss = self.wl.discriminator(wav.detach().squeeze(), real_GP.detach().squeeze()).mean()
                    else:
                        d_loss = self.wl.discriminator(wav.detach().squeeze(), y_pred.detach().squeeze()).mean()
                
                # regularization (ignore length variation)
                d_loss += loss_reg

                out_gt = self.wl.discriminator_forward(y_rec_gt.detach().squeeze())
                out_rec = self.wl.discriminator_forward(y_rec_gt_pred.detach().squeeze())

                # regularization (ignore reconstruction artifacts)
                d_loss += F.l1_loss(out_gt, out_rec)

            else:
                d_loss = self.wl.discriminator(wav.detach().squeeze(), y_pred.detach().squeeze()).mean()
        else:
            d_loss = 0
            
        # generator loss
        gen_loss = self.wl.generator(y_pred.squeeze())
        
        gen_loss = gen_loss.mean()
        
        return d_loss, gen_loss, y_pred.detach().cpu().numpy()
    
def length_to_mask(lengths):
    mask = torch.arange(lengths.max()).unsqueeze(0).expand(lengths.shape[0], -1).type_as(lengths)
    mask = torch.gt(mask+1, lengths.unsqueeze(1))
    return mask


# import torch
# import numpy as np
# import torch.nn.functional as F

# from accelerate import Accelerator, DistributedDataParallelKwargs
# from accelerate.utils import tqdm, ProjectConfiguration

# class SLMAdversarialLoss(torch.nn.Module):

#     def __init__(self, model, wl, sampler, min_len, max_len, batch_percentage=0.5, skip_update=10, sig=1.5):
#         super(SLMAdversarialLoss, self).__init__()
#         self.model = model
#         self.wl = wl
#         self.sampler = sampler
        
#         self.min_len = min_len
#         self.max_len = max_len
#         self.batch_percentage = batch_percentage
        
#         self.sig = sig
#         self.skip_update = skip_update
        
#     def forward(self, iters, accelerator, y_rec_gt, y_rec_gt_pred, waves, mel_input_length, ref_text, ref_lengths, use_ind, s_trg, ref_s=None):
#         text_mask = length_to_mask(ref_lengths).to(ref_text.device)
#         bert_dur = self.model.bert(ref_text, attention_mask=(~text_mask).int())
#         d_en = self.model.bert_encoder(bert_dur).transpose(-1, -2) 

#         if use_ind and np.random.rand() < 0.5:
#             s_preds = s_trg
#         else:
#             num_steps = np.random.randint(3, 5)
#             if ref_s is not None:
#                 s_preds = self.sampler(noise = torch.randn_like(s_trg).unsqueeze(1).to(ref_text.device), 
#                       embedding=bert_dur,
#                       embedding_scale=1,
#                                features=ref_s, # reference from the same speaker as the embedding
#                          embedding_mask_proba=0.1,
#                          num_steps=num_steps).squeeze(1)
#             else:
#                 s_preds = self.sampler(noise = torch.randn_like(s_trg).unsqueeze(1).to(ref_text.device), 
#                       embedding=bert_dur,
#                       embedding_scale=1,
#                          embedding_mask_proba=0.1,
#                          num_steps=num_steps).squeeze(1)
            
#         s_dur = s_preds[:, 128:]
#         s = s_preds[:, :128]
        
#         d, _ = self.model.predictor(d_en, s_dur, 
#                                                 ref_lengths, 
#                                                 torch.randn(ref_lengths.shape[0], ref_lengths.max(), 2).to(ref_text.device), 
#                                                 text_mask)
        
#         bib = 0

#         output_lengths = []
#         attn_preds = []
        
#         # differentiable duration modeling
#         for _s2s_pred, _text_length in zip(d, ref_lengths):

#             _s2s_pred_org = _s2s_pred[:_text_length, :]

#             _s2s_pred = torch.sigmoid(_s2s_pred_org)
#             _dur_pred = _s2s_pred.sum(axis=-1)

#             l = int(torch.round(_s2s_pred.sum()).item())
#             t = torch.arange(0, l).expand(l)

#             t = torch.arange(0, l).unsqueeze(0).expand((len(_s2s_pred), l)).to(ref_text.device)
#             loc = torch.cumsum(_dur_pred, dim=0) - _dur_pred / 2

#             h = torch.exp(-0.5 * torch.square(t - (l - loc.unsqueeze(-1))) / (self.sig)**2)

#             out = torch.nn.functional.conv1d(_s2s_pred_org.unsqueeze(0), 
#                                          h.unsqueeze(1), 
#                                          padding=h.shape[-1] - 1, groups=int(_text_length))[..., :l]
#             attn_preds.append(F.softmax(out.squeeze(), dim=0))

#             output_lengths.append(l)

#         max_len = max(output_lengths)
        
#         with torch.no_grad():
#             t_en = self.model.text_encoder(ref_text, ref_lengths, text_mask)
            
#         s2s_attn = torch.zeros(len(ref_lengths), int(ref_lengths.max()), max_len).to(ref_text.device)
#         for bib in range(len(output_lengths)):
#             s2s_attn[bib, :ref_lengths[bib], :output_lengths[bib]] = attn_preds[bib]

#         asr_pred = t_en @ s2s_attn

#         _, p_pred = self.model.predictor(d_en, s_dur, 
#                                                 ref_lengths, 
#                                                 s2s_attn, 
#                                                 text_mask)
        
#         mel_len = max(int(min(output_lengths) / 2 - 1), self.min_len // 2)
#         mel_len = min(mel_len, self.max_len // 2)
        
#         # get clips
        
#         en = []
#         p_en = []
#         sp = []
        
#         F0_fakes = []
#         N_fakes = []
        
#         wav = []

#         for bib in range(len(output_lengths)):
#             mel_length_pred = output_lengths[bib]
#             mel_length_gt = int(mel_input_length[bib].item() / 2)
#             if mel_length_gt <= mel_len or mel_length_pred <= mel_len:
#                 continue

#             sp.append(s_preds[bib])

#             random_start = np.random.randint(0, mel_length_pred - mel_len)
#             en.append(asr_pred[bib, :, random_start:random_start+mel_len])
#             p_en.append(p_pred[bib, :, random_start:random_start+mel_len])

#             # get ground truth clips
#             random_start = np.random.randint(0, mel_length_gt - mel_len)
#             y = waves[bib][(random_start * 2) * 300:((random_start+mel_len) * 2) * 300]
#             wav.append(torch.from_numpy(y).to(ref_text.device))
            
#             if len(wav) >= self.batch_percentage * len(waves): # prevent OOM due to longer lengths
#                 break
            
#         # global_min_batch = accelerator.gather(torch.tensor([len(wav)], device=ref_text.device)).min().item()
#         # if global_min_batch <= 1:
#         #     raise ValueError("skip slmadv")

#         if len(sp) <= 1:
#             return None
            
#         sp = torch.stack(sp)
#         wav = torch.stack(wav).float()
#         en = torch.stack(en)
#         p_en = torch.stack(p_en)

#         F0_fake, N_fake = self.model.predictor(texts=p_en, style=sp[:, 128:], f0=True)
#         y_pred = self.model.decoder(en, F0_fake, N_fake, sp[:, :128])
        
#         # discriminator loss
#         if (iters + 1) % self.skip_update == 0:
#             if np.random.randint(0, 2) == 0:
#                 wav = y_rec_gt_pred
#                 use_rec = True
#             else:
#                 use_rec = False

#             crop_size = min(wav.size(-1), y_pred.size(-1))
#             if use_rec: # use reconstructed (shorter lengths), do length invariant regularization
#                 if wav.size(-1) > y_pred.size(-1):
#                     real_GP = wav[:, : , :crop_size]
#                     out_crop = self.wl(wav = real_GP.detach().squeeze(),y_rec=None, discriminator_forward=True)
#                     out_org = self.wl(wav = wav.detach().squeeze(),y_rec=None, discriminator_forward=True)
#                     loss_reg = F.l1_loss(out_crop, out_org[..., :out_crop.size(-1)])

#                     if np.random.randint(0, 2) == 0:
#                         d_loss = self.wl(wav = real_GP.detach().squeeze(),y_rec= y_pred.detach().squeeze(), discriminator=True).mean()
#                     else:
#                         d_loss = self.wl(wav = wav.detach().squeeze(), y_rec = y_pred.detach().squeeze(), discriminator=True).mean()
#                 else:
#                     real_GP = y_pred[:, : , :crop_size]
#                     out_crop = self.wl(wav = real_GP.detach().squeeze(), y_rec=None, discriminator_forward=True)
#                     out_org = self.wl(wav = y_pred.detach().squeeze(),y_rec=None, discriminator_forward=True)
#                     loss_reg = F.l1_loss(out_crop, out_org[..., :out_crop.size(-1)])

#                     if np.random.randint(0, 2) == 0:
#                         d_loss = self.wl(wav = wav.detach().squeeze(), y_rec = real_GP.detach().squeeze(), discriminator=True ).mean()
#                     else:
#                         d_loss = self.wl(wav = wav.detach().squeeze(), y_rec = y_pred.detach().squeeze(), discriminator=True).mean()
                
#                 # regularization (ignore length variation)
#                 d_loss += loss_reg

#                 out_gt = self.wl(wav = y_rec_gt.detach().squeeze(),y_rec=None, discriminator_forward=True)
#                 out_rec = self.wl(wav = y_rec_gt_pred.detach().squeeze(), y_rec=None, discriminator_forward=True)

#                 # regularization (ignore reconstruction artifacts)
#                 d_loss += F.l1_loss(out_gt, out_rec)

#             else:
#                 d_loss = self.wl(wav = wav.detach().squeeze(),y_rec= y_pred.detach().squeeze(), discriminator=True).mean()
#         else:
#             d_loss = 0
            
#         # generator loss
#         gen_loss = self.wl(wav = None, y_rec = y_pred.squeeze(), generator=True)
        
#         gen_loss = gen_loss.mean()
        
        
#         return d_loss, gen_loss, y_pred.detach().cpu().numpy()
    
# def length_to_mask(lengths):
#     mask = torch.arange(lengths.max()).unsqueeze(0).expand(lengths.shape[0], -1).type_as(lengths)
#     mask = torch.gt(mask+1, lengths.unsqueeze(1))
#     return mask