File size: 20,764 Bytes
9e121db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import torch
from torch import nn
import torch.nn.functional as F
import torchaudio
from transformers import AutoModel, WhisperConfig, WhisperPreTrainedModel
import whisper

from transformers.models.whisper.modeling_whisper import WhisperEncoder


class SpectralConvergengeLoss(torch.nn.Module):
    """Spectral convergence loss module."""

    def __init__(self):
        """Initilize spectral convergence loss module."""
        super(SpectralConvergengeLoss, self).__init__()

    def forward(self, x_mag, y_mag):
        """Calculate forward propagation.
        Args:
            x_mag (Tensor): Magnitude spectrogram of predicted signal (B, #frames, #freq_bins).
            y_mag (Tensor): Magnitude spectrogram of groundtruth signal (B, #frames, #freq_bins).
        Returns:
            Tensor: Spectral convergence loss value.
        """
        return torch.norm(y_mag - x_mag, p=1) / torch.norm(y_mag, p=1)

class STFTLoss(torch.nn.Module):
    """STFT loss module."""

    def __init__(self, fft_size=1024, shift_size=120, win_length=600, window=torch.hann_window):
        """Initialize STFT loss module."""
        super(STFTLoss, self).__init__()
        self.fft_size = fft_size
        self.shift_size = shift_size
        self.win_length = win_length
        self.to_mel = torchaudio.transforms.MelSpectrogram(sample_rate=24000, n_fft=fft_size, win_length=win_length, hop_length=shift_size, window_fn=window)

        self.spectral_convergenge_loss = SpectralConvergengeLoss()

    def forward(self, x, y):
        """Calculate forward propagation.
        Args:
            x (Tensor): Predicted signal (B, T).
            y (Tensor): Groundtruth signal (B, T).
        Returns:
            Tensor: Spectral convergence loss value.
            Tensor: Log STFT magnitude loss value.
        """
        x_mag = self.to_mel(x)
        mean, std = -4, 4
        x_mag = (torch.log(1e-5 + x_mag) - mean) / std
        
        y_mag = self.to_mel(y)
        mean, std = -4, 4
        y_mag = (torch.log(1e-5 + y_mag) - mean) / std
        
        sc_loss = self.spectral_convergenge_loss(x_mag, y_mag)    
        return sc_loss


class MultiResolutionSTFTLoss(torch.nn.Module):
    """Multi resolution STFT loss module."""

    def __init__(self,
                 fft_sizes=[1024, 2048, 512],
                 hop_sizes=[120, 240, 50],
                 win_lengths=[600, 1200, 240],
                 window=torch.hann_window):
        """Initialize Multi resolution STFT loss module.
        Args:
            fft_sizes (list): List of FFT sizes.
            hop_sizes (list): List of hop sizes.
            win_lengths (list): List of window lengths.
            window (str): Window function type.
        """
        super(MultiResolutionSTFTLoss, self).__init__()
        assert len(fft_sizes) == len(hop_sizes) == len(win_lengths)
        self.stft_losses = torch.nn.ModuleList()
        for fs, ss, wl in zip(fft_sizes, hop_sizes, win_lengths):
            self.stft_losses += [STFTLoss(fs, ss, wl, window)]

    def forward(self, x, y):
        """Calculate forward propagation.
        Args:
            x (Tensor): Predicted signal (B, T).
            y (Tensor): Groundtruth signal (B, T).
        Returns:
            Tensor: Multi resolution spectral convergence loss value.
            Tensor: Multi resolution log STFT magnitude loss value.
        """
        sc_loss = 0.0
        for f in self.stft_losses:
            sc_l = f(x, y)
            sc_loss += sc_l
        sc_loss /= len(self.stft_losses)

        return sc_loss
    
    
def feature_loss(fmap_r, fmap_g):
    loss = 0
    for dr, dg in zip(fmap_r, fmap_g):
        for rl, gl in zip(dr, dg):
            loss += torch.mean(torch.abs(rl - gl))

    return loss*2


def discriminator_loss(disc_real_outputs, disc_generated_outputs):
    loss = 0
    r_losses = []
    g_losses = []
    for dr, dg in zip(disc_real_outputs, disc_generated_outputs):
        r_loss = torch.mean((1-dr)**2)
        g_loss = torch.mean(dg**2)
        loss += (r_loss + g_loss)
        r_losses.append(r_loss.item())
        g_losses.append(g_loss.item())

    return loss, r_losses, g_losses


def generator_loss(disc_outputs):
    loss = 0
    gen_losses = []
    for dg in disc_outputs:
        l = torch.mean((1-dg)**2)
        gen_losses.append(l)
        loss += l

    return loss, gen_losses

""" https://dl.acm.org/doi/abs/10.1145/3573834.3574506 """
def discriminator_TPRLS_loss(disc_real_outputs, disc_generated_outputs):
    loss = 0
    for dr, dg in zip(disc_real_outputs, disc_generated_outputs):
        tau = 0.04
        m_DG = torch.median((dr-dg))
        L_rel = torch.mean((((dr - dg) - m_DG)**2)[dr < dg + m_DG])
        loss += tau - F.relu(tau - L_rel)
    return loss

def generator_TPRLS_loss(disc_real_outputs, disc_generated_outputs):
    loss = 0
    for dg, dr in zip(disc_real_outputs, disc_generated_outputs):
        tau = 0.04
        m_DG = torch.median((dr-dg))
        L_rel = torch.mean((((dr - dg) - m_DG)**2)[dr < dg + m_DG])
        loss += tau - F.relu(tau - L_rel)
    return loss

class GeneratorLoss(torch.nn.Module):

    def __init__(self, mpd, msd):
        super(GeneratorLoss, self).__init__()
        self.mpd = mpd
        self.msd = msd
        
    def forward(self, y, y_hat):
        y_df_hat_r, y_df_hat_g, fmap_f_r, fmap_f_g = self.mpd(y, y_hat)
        y_ds_hat_r, y_ds_hat_g, fmap_s_r, fmap_s_g = self.msd(y, y_hat)
        loss_fm_f = feature_loss(fmap_f_r, fmap_f_g)
        loss_fm_s = feature_loss(fmap_s_r, fmap_s_g)
        loss_gen_f, losses_gen_f = generator_loss(y_df_hat_g)
        loss_gen_s, losses_gen_s = generator_loss(y_ds_hat_g)

        loss_rel = generator_TPRLS_loss(y_df_hat_r, y_df_hat_g) + generator_TPRLS_loss(y_ds_hat_r, y_ds_hat_g)
        
        loss_gen_all = loss_gen_s + loss_gen_f + loss_fm_s + loss_fm_f + loss_rel
        
        return loss_gen_all.mean()
    
class DiscriminatorLoss(torch.nn.Module):

    def __init__(self, mpd, msd):
        super(DiscriminatorLoss, self).__init__()
        self.mpd = mpd
        self.msd = msd
        
    def forward(self, y, y_hat):
        # MPD
        y_df_hat_r, y_df_hat_g, _, _ = self.mpd(y, y_hat)
        loss_disc_f, losses_disc_f_r, losses_disc_f_g = discriminator_loss(y_df_hat_r, y_df_hat_g)
        # MSD
        y_ds_hat_r, y_ds_hat_g, _, _ = self.msd(y, y_hat)
        loss_disc_s, losses_disc_s_r, losses_disc_s_g = discriminator_loss(y_ds_hat_r, y_ds_hat_g)
        
        loss_rel = discriminator_TPRLS_loss(y_df_hat_r, y_df_hat_g) + discriminator_TPRLS_loss(y_ds_hat_r, y_ds_hat_g)


        d_loss = loss_disc_s + loss_disc_f + loss_rel
        
        return d_loss.mean()
    
    


# #####################
# MIXED PRECISION


class WhisperEncoderOnly(WhisperPreTrainedModel):
    def __init__(self, config: WhisperConfig):
        super().__init__(config)
        self.encoder = WhisperEncoder(config)

    def forward(self, input_features, attention_mask=None):
        return self.encoder(input_features, attention_mask)



class WavLMLoss(torch.nn.Module):
    def __init__(self, model, wd, model_sr, slm_sr=16000):
        super(WavLMLoss, self).__init__()
    
        config = WhisperConfig.from_pretrained("Respair/Whisper_Large_v2_Encoder_Block")

        # this will load the full model and keep only the encoder
        full_model = WhisperEncoderOnly.from_pretrained("openai/whisper-large-v2", config=config, device_map='auto',torch_dtype=torch.bfloat16)
        model = WhisperEncoderOnly(config)
        model.encoder.load_state_dict(full_model.encoder.state_dict())
        del full_model

        
        self.wavlm = model.to(torch.bfloat16)
        self.wd = wd
        self.resample = torchaudio.transforms.Resample(model_sr, slm_sr)

    def forward(self, wav,  y_rec, generator=False, discriminator=False, discriminator_forward=False):
        
        if generator:
            y_rec = y_rec.squeeze(1)
            

            y_rec = whisper.pad_or_trim(y_rec)
            y_rec = whisper.log_mel_spectrogram(y_rec)

            with torch.no_grad():
                y_rec_embeddings = self.wavlm.encoder(y_rec.to(torch.bfloat16), output_hidden_states=True).hidden_states
            y_rec_embeddings = torch.stack(y_rec_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)
            y_df_hat_g = self.wd(y_rec_embeddings.to(torch.float32))
            loss_gen = torch.mean((1-y_df_hat_g)**2)
            
            return loss_gen.to(torch.float32)
        
        elif discriminator:
            
            wav = wav.squeeze(1)
            y_rec = y_rec.squeeze(1)

            wav = whisper.pad_or_trim(wav)
            wav = whisper.log_mel_spectrogram(wav)

            y_rec = whisper.pad_or_trim(y_rec)
            y_rec = whisper.log_mel_spectrogram(y_rec)

            with torch.no_grad():
                wav_embeddings = self.wavlm.encoder(wav.to(torch.bfloat16), output_hidden_states=True).hidden_states
                y_rec_embeddings = self.wavlm.encoder(y_rec.to(torch.bfloat16), output_hidden_states=True).hidden_states

                y_embeddings = torch.stack(wav_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)
                y_rec_embeddings = torch.stack(y_rec_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)

            y_d_rs = self.wd(y_embeddings.to(torch.float32))
            y_d_gs = self.wd(y_rec_embeddings.to(torch.float32))
            
            y_df_hat_r, y_df_hat_g = y_d_rs, y_d_gs
            
            r_loss = torch.mean((1-y_df_hat_r)**2)
            g_loss = torch.mean((y_df_hat_g)**2)
            
            loss_disc_f = r_loss + g_loss
                            
            return loss_disc_f.mean().to(torch.float32)
        
        
        
        elif discriminator_forward:
            # Squeeze the channel dimension if it's unnecessary
            wav = wav.squeeze(1) # Adjust this line if the channel dimension is not at dim=1


            with torch.no_grad():
                
                wav_16 = self.resample(wav)
                wav_16 = whisper.pad_or_trim(wav_16)
                wav_16 = whisper.log_mel_spectrogram(wav_16)
                
                wav_embeddings = self.wavlm.encoder(wav_16.to(torch.bfloat16) , output_hidden_states=True).hidden_states
                y_embeddings = torch.stack(wav_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)

            y_d_rs = self.wd(y_embeddings.to(torch.float32))
            
            return y_d_rs
        
        else:
        
            wav = wav.squeeze(1)
            y_rec = y_rec.squeeze(1)

            wav = whisper.pad_or_trim(wav)
            wav = whisper.log_mel_spectrogram(wav)

            y_rec = whisper.pad_or_trim(y_rec)
            y_rec = whisper.log_mel_spectrogram(y_rec)

            with torch.no_grad():
                wav_embeddings = self.wavlm.encoder(wav.to(torch.bfloat16), output_hidden_states=True).hidden_states
        
                y_rec_embeddings = self.wavlm.encoder(y_rec.to(torch.bfloat16), output_hidden_states=True).hidden_states


            floss = 0
            for er, eg in zip([e.to(torch.float32) for e in wav_embeddings], [e.to(torch.float32) for e in y_rec_embeddings]):
                floss += torch.mean(torch.abs(er - eg))
            
            return floss.mean()



    def generator(self, y_rec):
        
        y_rec = y_rec.squeeze(1)
        

        y_rec = whisper.pad_or_trim(y_rec)
        y_rec = whisper.log_mel_spectrogram(y_rec)

        with torch.no_grad():
            y_rec_embeddings = self.wavlm.encoder(y_rec.to(torch.bfloat16), output_hidden_states=True).hidden_states
        y_rec_embeddings = torch.stack(y_rec_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)
        y_df_hat_g = self.wd(y_rec_embeddings.to(torch.float32))
        loss_gen = torch.mean((1-y_df_hat_g)**2)
        
        return loss_gen.to(torch.float32)

    def discriminator(self, wav, y_rec):
        
        wav = wav.squeeze(1)
        y_rec = y_rec.squeeze(1)

        wav = whisper.pad_or_trim(wav)
        wav = whisper.log_mel_spectrogram(wav)

        y_rec = whisper.pad_or_trim(y_rec)
        y_rec = whisper.log_mel_spectrogram(y_rec)

        with torch.no_grad():
            wav_embeddings = self.wavlm.encoder(wav.to(torch.bfloat16), output_hidden_states=True).hidden_states
            y_rec_embeddings = self.wavlm.encoder(y_rec.to(torch.bfloat16), output_hidden_states=True).hidden_states

            y_embeddings = torch.stack(wav_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)
            y_rec_embeddings = torch.stack(y_rec_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)

        y_d_rs = self.wd(y_embeddings.to(torch.float32))
        y_d_gs = self.wd(y_rec_embeddings.to(torch.float32))
        
        y_df_hat_r, y_df_hat_g = y_d_rs, y_d_gs
        
        r_loss = torch.mean((1-y_df_hat_r)**2)
        g_loss = torch.mean((y_df_hat_g)**2)
        
        loss_disc_f = r_loss + g_loss
                        
        return loss_disc_f.mean().to(torch.float32)
    
    


    def discriminator_forward(self, wav):
        # Squeeze the channel dimension if it's unnecessary
        wav = wav.squeeze(1) # Adjust this line if the channel dimension is not at dim=1


        with torch.no_grad():
            
            wav_16 = self.resample(wav)
            wav_16 = whisper.pad_or_trim(wav_16)
            wav_16 = whisper.log_mel_spectrogram(wav_16)
            
            wav_embeddings = self.wavlm.encoder(wav_16.to(torch.bfloat16) , output_hidden_states=True).hidden_states
            y_embeddings = torch.stack(wav_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)

        y_d_rs = self.wd(y_embeddings.to(torch.float32))
        
        return y_d_rs
    




################### Whisper - No Mixed Precision!

# class WhisperEncoderOnly(WhisperPreTrainedModel):
#     def __init__(self, config: WhisperConfig):
#         super().__init__(config)
#         self.encoder = WhisperEncoder(config)

#     def forward(self, input_features, attention_mask=None):
#         return self.encoder(input_features, attention_mask)

# class WavLMLoss(torch.nn.Module):
        
#     def __init__(self, model, wd, model_sr, slm_sr=16000):
#         super(WavLMLoss, self).__init__()
    
#         config = WhisperConfig.from_pretrained("Respair/Whisper_Large_v2_Encoder_Block")

#         # this will load the full model and keep only the encoder
#         full_model = WhisperEncoderOnly.from_pretrained("openai/whisper-large-v2", config=config, device_map='auto')
#         model = WhisperEncoderOnly(config)
#         model.encoder.load_state_dict(full_model.encoder.state_dict())
#         del full_model

        
#         self.wavlm = model
#         self.wd = wd
#         self.resample = torchaudio.transforms.Resample(model_sr, slm_sr)

#     def forward(self, wav, y_rec):
        
#         wav = wav.squeeze(1)  
#         y_rec = y_rec.squeeze(1)  

#         wav = whisper.pad_or_trim(wav)
#         wav = whisper.log_mel_spectrogram(wav)

#         y_rec = whisper.pad_or_trim(y_rec)
#         y_rec = whisper.log_mel_spectrogram(y_rec)

#         with torch.no_grad():
#             wav_embeddings = self.wavlm.encoder(wav, output_hidden_states=True).hidden_states
#             y_rec_embeddings = self.wavlm.encoder(y_rec, output_hidden_states=True).hidden_states

#         floss = 0
#         for er, eg in zip(wav_embeddings, y_rec_embeddings):
#             floss += torch.mean(torch.abs(er - eg))
        
#         return floss.mean()

#     def generator(self, y_rec):
        
#         y_rec = y_rec.squeeze(1)  

#         y_rec = whisper.pad_or_trim(y_rec)
#         y_rec = whisper.log_mel_spectrogram(y_rec)

#         with torch.no_grad():
#             y_rec_embeddings = self.wavlm.encoder(y_rec, output_hidden_states=True).hidden_states
#         y_rec_embeddings = torch.stack(y_rec_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)
#         y_df_hat_g = self.wd(y_rec_embeddings)
#         loss_gen = torch.mean((1-y_df_hat_g)**2)
        
#         return loss_gen

#     def discriminator(self, wav, y_rec):
        
#         wav = wav.squeeze(1)  
#         y_rec = y_rec.squeeze(1) 

#         wav = whisper.pad_or_trim(wav)
#         wav = whisper.log_mel_spectrogram(wav)

#         y_rec = whisper.pad_or_trim(y_rec)
#         y_rec = whisper.log_mel_spectrogram(y_rec)

#         with torch.no_grad():
#             wav_embeddings = self.wavlm.encoder(wav, output_hidden_states=True).hidden_states
#             y_rec_embeddings = self.wavlm.encoder(y_rec, output_hidden_states=True).hidden_states

#             y_embeddings = torch.stack(wav_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)
#             y_rec_embeddings = torch.stack(y_rec_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)

#         y_d_rs = self.wd(y_embeddings)
#         y_d_gs = self.wd(y_rec_embeddings)
        
#         y_df_hat_r, y_df_hat_g = y_d_rs, y_d_gs
        
#         r_loss = torch.mean((1-y_df_hat_r)**2)
#         g_loss = torch.mean((y_df_hat_g)**2)
        
#         loss_disc_f = r_loss + g_loss
                        
#         return loss_disc_f.mean()


#     def discriminator_forward(self, wav):
#         # Squeeze the channel dimension if it's unnecessary
#         wav = wav.squeeze(1)  # Adjust this line if the channel dimension is not at dim=1


#         with torch.no_grad():

#             wav_16 = self.resample(wav)
#             wav_16 = whisper.pad_or_trim(wav_16)
#             wav_16 = whisper.log_mel_spectrogram(wav_16)

#             wav_embeddings = self.wavlm.encoder(wav_16, output_hidden_states=True).hidden_states
#             y_embeddings = torch.stack(wav_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)

#         y_d_rs = self.wd(y_embeddings)

#         return y_d_rs


    
###### ORIGINAL
# class WavLMLoss(torch.nn.Module):

#     def __init__(self, model, wd, model_sr, slm_sr=16000):
#         super(WavLMLoss, self).__init__()
#         self.wavlm = AutoModel.from_pretrained(model)
#         self.wd = wd
#         self.resample = torchaudio.transforms.Resample(model_sr, slm_sr)
     
#     def forward(self, wav, y_rec):
        
  
        
#         with torch.no_grad():
#             wav_16 = self.resample(wav)
#             wav_embeddings = self.wavlm(wav_16, output_hidden_states=True).hidden_states
#         y_rec_16 = self.resample(y_rec)
#         y_rec_embeddings = self.wavlm(y_rec_16.squeeze(), output_hidden_states=True).hidden_states

#         floss = 0
#         for er, eg in zip(wav_embeddings, y_rec_embeddings):
#             floss += torch.mean(torch.abs(er - eg))
        
#         return floss.mean()
    
#     def generator(self, y_rec):
#         y_rec_16 = self.resample(y_rec)
#         y_rec_embeddings = self.wavlm(y_rec_16, output_hidden_states=True).hidden_states
#         y_rec_embeddings = torch.stack(y_rec_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)
#         y_df_hat_g = self.wd(y_rec_embeddings)
#         loss_gen = torch.mean((1-y_df_hat_g)**2)
        
#         return loss_gen
    
#     def discriminator(self, wav, y_rec):
#         with torch.no_grad():
#             wav_16 = self.resample(wav)
#             wav_embeddings = self.wavlm(wav_16, output_hidden_states=True).hidden_states
#             y_rec_16 = self.resample(y_rec)
#             y_rec_embeddings = self.wavlm(y_rec_16, output_hidden_states=True).hidden_states

#             y_embeddings = torch.stack(wav_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)
#             y_rec_embeddings = torch.stack(y_rec_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)

#         y_d_rs = self.wd(y_embeddings)
#         y_d_gs = self.wd(y_rec_embeddings)
        
#         y_df_hat_r, y_df_hat_g = y_d_rs, y_d_gs
        
#         r_loss = torch.mean((1-y_df_hat_r)**2)
#         g_loss = torch.mean((y_df_hat_g)**2)
        
#         loss_disc_f = r_loss + g_loss
                        
#         return loss_disc_f.mean()

#     def discriminator_forward(self, wav):
#         with torch.no_grad():
#             wav_16 = self.resample(wav)
#             wav_embeddings = self.wavlm(wav_16, output_hidden_states=True).hidden_states
#             y_embeddings = torch.stack(wav_embeddings, dim=1).transpose(-1, -2).flatten(start_dim=1, end_dim=2)

#         y_d_rs = self.wd(y_embeddings)
        
#         return y_d_rs