-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
1278 lines (1099 loc) · 57.2 KB
/
Copy pathmodel.py
File metadata and controls
1278 lines (1099 loc) · 57.2 KB
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from einops import rearrange
from contextlib import contextmanager
from functools import partial
import numpy as np
import warnings
import sys
import os
import torchaudio
# --- Official AudioSR Imports (or their equivalents) ---
from audiosr.latent_diffusion.util import (
exists,
default,
instantiate_from_config,
)
from audiosr.latent_diffusion.modules.distributions.distributions import (
DiagonalGaussianDistribution,
)
from audiosr.latent_diffusion.modules.diffusionmodules.util import (
make_beta_schedule,
extract_into_tensor,
noise_like,
checkpoint,
conv_nd,
linear,
zero_module,
normalization,
timestep_embedding,
)
from audiosr.latent_diffusion.modules.attention import SpatialTransformer
from audiosr.latent_diffusion.modules.diffusionmodules.model import Encoder, Decoder # VAE components
# ==============================================================================
# SECTION: SPECTRAL LOSS FUNCTIONS (直接集成在此文件中)
# ==============================================================================
def spectral_convergence_loss(x, y):
"""计算频谱收敛损失 (Spectral Convergence Loss)"""
return torch.norm(torch.abs(y) - torch.abs(x), p="fro") / torch.norm(torch.abs(y), p="fro")
def log_stft_magnitude_loss(x, y):
"""计算对数STFT幅值损失 (Log STFT Magnitude Loss)"""
x_mag = torch.abs(x) + 1e-8
y_mag = torch.abs(y) + 1e-8
return F.l1_loss(torch.log(y_mag), torch.log(x_mag))
class STFTLoss(nn.Module):
"""单一分辨率的STFT损失模块"""
def __init__(self, fft_size, hop_size, win_length, window):
super().__init__()
self.fft_size = fft_size
self.hop_size = hop_size
self.win_length = win_length
self.window = getattr(torch, window)(win_length)
def forward(self, x_wav, y_wav):
self.window = self.window.to(x_wav.device)
x_stft = torch.stft(x_wav.squeeze(1), n_fft=self.fft_size, hop_length=self.hop_size,
win_length=self.win_length, window=self.window, return_complex=True)
y_stft = torch.stft(y_wav.squeeze(1), n_fft=self.fft_size, hop_length=self.hop_size,
win_length=self.win_length, window=self.window, return_complex=True)
sc_loss = spectral_convergence_loss(x_stft, y_stft)
mag_loss = log_stft_magnitude_loss(x_stft, y_stft)
return sc_loss, mag_loss
class MultiResolutionSTFTLoss(nn.Module):
"""多分辨率STFT损失"""
def __init__(self, fft_sizes=[1024, 2048, 512], hop_sizes=[120, 240, 50], win_lengths=[600, 1200, 240], window="hann_window"):
super().__init__()
self.stft_losses = nn.ModuleList()
for fs, hs, wl in zip(fft_sizes, hop_sizes, win_lengths):
self.stft_losses.append(STFTLoss(fs, hs, wl, window))
def forward(self, x_wav, y_wav):
sc_loss, mag_loss = 0.0, 0.0
for f in self.stft_losses:
sc_l, mag_l = f(x_wav, y_wav)
sc_loss += sc_l
mag_loss += mag_l
sc_loss /= len(self.stft_losses)
mag_loss /= len(self.stft_losses)
return sc_loss, mag_loss
class MelSpectrogramLoss(nn.Module):
"""梅尔频谱图的L1损失"""
def __init__(self, sample_rate, n_fft, hop_length, win_length, n_mels, f_min, f_max):
super().__init__()
self.mel_transform = torchaudio.transforms.MelSpectrogram(
sample_rate=sample_rate, n_fft=n_fft, hop_length=hop_length,
win_length=win_length, n_mels=n_mels, f_min=f_min, f_max=f_max,
power=1.0, norm='slaney'
)
def forward(self, x_wav, y_wav):
x_mel = self.mel_transform(x_wav.squeeze(1))
y_mel = self.mel_transform(y_wav.squeeze(1))
return F.l1_loss(torch.log(torch.clamp(x_mel, min=1e-5)),
torch.log(torch.clamp(y_mel, min=1e-5)))
# ==============================================================================
# SECTION: MODEL DEFINITIONS
# ==============================================================================
# === GLOBAL ENCODER FOR WHOLE AUDIO PROCESSING ===
class GlobalAudioEncoder(nn.Module):
"""
Lightweight CNN+Transformer Global Encoder that processes whole audio
and extracts critical global information to condition the diffusion process.
"""
def __init__(self,
input_channels=256, # Mel spectrogram channels
hidden_dim=128, # Hidden dimension for efficiency
num_heads=4, # Multi-head attention
num_layers=4, # Transformer layers
context_dim=64, # Output context dimension
max_seq_len=1024): # Maximum sequence length
super().__init__()
self.hidden_dim = hidden_dim
self.context_dim = context_dim
# === CNN Feature Extractor ===
# Efficiently downsample and extract features from mel spectrogram
self.cnn_encoder = nn.Sequential(
# First conv block - reduce frequency dimension
nn.Conv2d(1, 32, kernel_size=(8, 3), stride=(4, 1), padding=(2, 1)),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
# Second conv block - further downsample
nn.Conv2d(32, 64, kernel_size=(4, 3), stride=(2, 1), padding=(1, 1)),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
# Third conv block - final feature extraction
nn.Conv2d(64, hidden_dim, kernel_size=(4, 3), stride=(2, 1), padding=(1, 1)),
nn.BatchNorm2d(hidden_dim),
nn.ReLU(inplace=True),
)
# === Positional Encoding ===
self.pos_encoding = nn.Parameter(torch.randn(1, max_seq_len, hidden_dim) * 0.02)
# === Transformer Encoder ===
encoder_layer = nn.TransformerEncoderLayer(
d_model=hidden_dim,
nhead=num_heads,
dim_feedforward=hidden_dim * 2,
dropout=0.1,
activation='gelu',
batch_first=True
)
self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=num_layers)
# === Output Projection ===
self.output_proj = nn.Sequential(
nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(inplace=True),
nn.Dropout(0.1),
nn.Linear(hidden_dim, context_dim)
)
# === Global Context Pooling ===
self.global_pool = nn.AdaptiveAvgPool1d(1)
self.global_proj = nn.Linear(context_dim, context_dim)
def forward(self, x):
"""
Forward pass of Global Encoder
Args:
x: Input mel spectrogram [batch_size, 1, mel_bins, time_steps]
Returns:
global_context: Global context features [batch_size, context_dim]
sequence_context: Sequence context features [batch_size, seq_len, context_dim]
"""
batch_size = x.shape[0]
# === CNN Feature Extraction ===
# x: [B, 1, mel_bins, time] -> [B, hidden_dim, reduced_mel, time]
features = self.cnn_encoder(x) # [B, hidden_dim, ~16, time]
# Reshape for transformer: [B, hidden_dim, reduced_mel, time] -> [B, seq_len, hidden_dim]
B, C, H, W = features.shape
features = features.permute(0, 3, 1, 2).contiguous() # [B, time, hidden_dim, reduced_mel]
features = features.view(B, W, C * H) # [B, time, hidden_dim * reduced_mel]
# Project to hidden_dim
if C * H != self.hidden_dim:
if not hasattr(self, '_adaptive_proj'):
self._adaptive_proj = nn.Linear(C * H, self.hidden_dim).to(features.device)
features = self._adaptive_proj(features)
# === Add Positional Encoding ===
seq_len = features.shape[1]
if seq_len <= self.pos_encoding.shape[1]:
pos_enc = self.pos_encoding[:, :seq_len, :]
else:
# If sequence is longer, interpolate positional encoding
pos_enc = F.interpolate(
self.pos_encoding.transpose(1, 2),
size=seq_len,
mode='linear',
align_corners=False
).transpose(1, 2)
features = features + pos_enc
# === Transformer Processing ===
# Self-attention to capture global dependencies
transformed_features = self.transformer(features) # [B, seq_len, hidden_dim]
# === Output Projections ===
sequence_context = self.output_proj(transformed_features) # [B, seq_len, context_dim]
# Global context via pooling
global_features = self.global_pool(sequence_context.transpose(1, 2)).squeeze(-1) # [B, context_dim]
global_context = self.global_proj(global_features) # [B, context_dim]
return global_context, sequence_context
class GlobalConditionedSpatialTransformer(SpatialTransformer):
"""
Enhanced SpatialTransformer that incorporates global audio context
"""
def __init__(self, in_channels, n_heads, d_head, depth=1, dropout=0., context_dim=None, global_context_dim=64):
super().__init__(in_channels, n_heads, d_head, depth, dropout, context_dim)
self.global_context_dim = global_context_dim
if global_context_dim is not None:
# Project global context to match transformer dimensions
self.global_proj = nn.Linear(global_context_dim, n_heads * d_head)
# Global context attention
self.global_attn = nn.MultiheadAttention(
embed_dim=n_heads * d_head,
num_heads=n_heads,
dropout=dropout,
batch_first=True
)
def forward(self, x, context=None, global_context=None):
"""
Forward pass with global context integration
Args:
x: Input features [B, C, H, W]
context: Local context from other conditions
global_context: Global audio context [B, global_context_dim] or [B, seq_len, global_context_dim]
"""
if global_context is not None and hasattr(self, 'global_proj'):
# Process global context
if global_context.dim() == 2:
# Single global vector: [B, global_context_dim] -> [B, 1, n_heads * d_head]
global_features = self.global_proj(global_context).unsqueeze(1)
else:
# Sequence of global vectors: [B, seq_len, global_context_dim] -> [B, seq_len, n_heads * d_head]
global_features = self.global_proj(global_context)
# Reshape input for attention
B, C, H, W = x.shape
x_flat = x.view(B, C, H * W).transpose(1, 2) # [B, H*W, C]
# Apply global context attention
attended_x, _ = self.global_attn(x_flat, global_features, global_features)
# Reshape back
x += attended_x.transpose(1, 2).view(B, C, H, W)
# Call parent forward with potentially modified x
return super().forward(x, context)
# === EXACT AUDIOSR VOCODER ===
class AudioSRVocoder(nn.Module):
"""
Exact AudioSR HiFi-GAN vocoder implementation matching output size perfectly
"""
def __init__(self):
super().__init__()
# Pre-conv
self.conv_pre = nn.Conv1d(256, 1536, 7, 1, padding=3)
# Upsampling - EXACT AudioSR configuration
self.ups = nn.ModuleList([
nn.ConvTranspose1d(1536, 768, 12, 6, padding=3),
nn.ConvTranspose1d(768, 384, 10, 5, padding=2, output_padding=1),
nn.ConvTranspose1d(384, 192, 8, 4, padding=2),
nn.ConvTranspose1d(192, 96, 4, 2, padding=1),
nn.ConvTranspose1d(96, 48, 4, 2, padding=1),
])
# ResBlocks - EXACT AudioSR structure (20 total)
self.resblocks = nn.ModuleList()
channels_list = [768, 768, 768, 768, 384, 384, 384, 384, 192, 192, 192, 192, 96, 96, 96, 96, 48, 48, 48, 48]
kernels_list = [3, 7, 11, 15, 3, 7, 11, 15, 3, 7, 11, 15, 3, 7, 11, 15, 3, 7, 11, 15]
for channels, kernel_size in zip(channels_list, kernels_list):
self.resblocks.append(AudioSRVocoderResBlock(channels, kernel_size))
# Post conv
self.conv_post = nn.Conv1d(48, 1, 7, 1, padding=3)
# Don't apply weight norm during initialization - will be applied after weight loading
self._weight_norm_applied = False
def apply_weight_norm(self):
"""Apply weight normalization to all conv layers"""
from torch.nn.utils import weight_norm
self.conv_pre = weight_norm(self.conv_pre)
for up in self.ups:
up = weight_norm(up)
for resblock in self.resblocks:
resblock.apply_weight_norm()
self.conv_post = weight_norm(self.conv_post)
def forward(self, x):
x = self.conv_pre(x)
for i in range(len(self.ups)):
x = F.leaky_relu(x, 0.1)
x = self.ups[i](x)
# Apply corresponding resblocks
xs = None
for j in range(4): # 4 resblocks per upsampling stage
resblock_idx = i * 4 + j
if xs is None:
xs = self.resblocks[resblock_idx](x)
else:
xs += self.resblocks[resblock_idx](x)
x = xs / 4
x = F.leaky_relu(x, 0.1)
x = self.conv_post(x)
x = torch.tanh(x)
return x
def remove_weight_norm(self):
"""Remove weight normalization for compatibility"""
from torch.nn.utils import remove_weight_norm
try:
remove_weight_norm(self.conv_pre)
except ValueError:
pass
for up in self.ups:
try:
remove_weight_norm(up)
except ValueError:
pass
for resblock in self.resblocks:
resblock.remove_weight_norm()
try:
remove_weight_norm(self.conv_post)
except ValueError:
pass
class AudioSRVocoderResBlock(nn.Module):
"""HiFi-GAN ResBlock for vocoder"""
def __init__(self, channels, kernel_size, dilation=(1, 3, 5)):
super().__init__()
def get_padding(kernel_size, dilation=1):
return int((kernel_size * dilation - dilation) / 2)
self.convs1 = nn.ModuleList([
nn.Conv1d(channels, channels, kernel_size, 1,
padding=get_padding(kernel_size, d), dilation=d)
for d in dilation
])
self.convs2 = nn.ModuleList([
nn.Conv1d(channels, channels, kernel_size, 1,
padding=get_padding(kernel_size, 1), dilation=1)
for _ in dilation
])
def apply_weight_norm(self):
"""Apply weight normalization to all conv layers"""
from torch.nn.utils import weight_norm
for i in range(len(self.convs1)):
self.convs1[i] = weight_norm(self.convs1[i])
self.convs2[i] = weight_norm(self.convs2[i])
def remove_weight_norm(self):
"""Remove weight normalization"""
from torch.nn.utils import remove_weight_norm
for i in range(len(self.convs1)):
try:
remove_weight_norm(self.convs1[i])
except ValueError:
pass
try:
remove_weight_norm(self.convs2[i])
except ValueError:
pass
def forward(self, x):
for c1, c2 in zip(self.convs1, self.convs2):
xt = F.leaky_relu(x, 0.1)
xt = c1(xt)
xt = F.leaky_relu(xt, 0.1)
xt = c2(xt)
x = xt + x
return x
# --- Import AudioSR's HiFi-GAN Vocoder ---
# We'll use the original AudioSR HiFi-GAN implementation
try:
import sys
import os
sys.path.append('/home/husrcf/Code/Python/projectlily Z')
from audiosr.hifigan.models import Generator as HiFiGANGenerator
from audiosr.utilities.model import get_vocoder_config_48k, vocoder_infer
from audiosr.hifigan import AttrDict
AUDIOSR_AVAILABLE = True
except ImportError:
print("Warning: AudioSR not available, using fallback vocoder")
AUDIOSR_AVAILABLE = False
# Fallback simplified vocoder for when AudioSR is not available
class HiFiGANGenerator(nn.Module):
def __init__(self, config):
super().__init__()
self.conv_pre = nn.Conv1d(256, 512, 7, 1, padding=3)
self.conv_post = nn.Conv1d(32, 1, 7, 1, padding=3)
def forward(self, x):
return torch.tanh(self.conv_post(F.leaky_relu(self.conv_pre(x))))
def remove_weight_norm(self): pass
# === EXACT AUDIOSR CLAP WITH ALL MISSING MODULES ===
class DropPath(nn.Module):
"""Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks)."""
def __init__(self, drop_prob=None):
super(DropPath, self).__init__()
self.drop_prob = drop_prob
def forward(self, x):
if self.drop_prob == 0. or not self.training:
return x
keep_prob = 1 - self.drop_prob
shape = (x.shape[0],) + (1,) * (x.ndim - 1) # work with diff dim tensors, not just 2D ConvNets
random_tensor = keep_prob + torch.rand(shape, dtype=x.dtype, device=x.device)
random_tensor.floor_() # binarize
output = x.div(keep_prob) * random_tensor
return output
class Mlp(nn.Module):
def __init__(self, in_features, hidden_features, out_features, act_layer=nn.GELU, drop=0.):
super().__init__()
self.fc1 = nn.Linear(in_features, hidden_features)
self.act = act_layer()
self.fc2 = nn.Linear(hidden_features, out_features)
self.drop = nn.Dropout(drop)
class WindowAttention(nn.Module):
def __init__(self, dim, num_heads):
super().__init__()
self.qkv = nn.Linear(dim, dim * 3, bias=True)
self.proj = nn.Linear(dim, dim)
# MISSING MODULES ADDED:
self.attn_drop = nn.Dropout(0.0) # Attention dropout
self.proj_drop = nn.Dropout(0.0) # Projection dropout
self.softmax = nn.Softmax(dim=-1) # Softmax activation
window_size = 8
self.relative_position_bias_table = nn.Parameter(
torch.zeros((2 * window_size - 1) * (2 * window_size - 1), num_heads))
self.relative_position_index = nn.Parameter(torch.zeros(64, 64), requires_grad=False)
def forward(self, x):
# Simplified forward pass - full implementation would be more complex
qkv = self.qkv(x)
q, k, v = qkv.chunk(3, dim=-1)
# Attention calculation
attn = (q @ k.transpose(-2, -1))
attn = self.softmax(attn)
attn = self.attn_drop(attn)
x = (attn @ v)
x = self.proj(x)
x = self.proj_drop(x)
return x
class SwinTransformerBlock(nn.Module):
def __init__(self, dim, num_heads):
super().__init__()
self.norm1 = nn.LayerNorm(dim)
self.attn = WindowAttention(dim, num_heads)
self.norm2 = nn.LayerNorm(dim)
# MISSING MODULE ADDED:
self.drop_path = DropPath(0.1) # DropPath for stochastic depth
self.mlp = Mlp(dim, dim * 4, dim)
self.register_parameter("attn_mask", None)
def forward(self, x):
x = x + self.drop_path(self.attn(self.norm1(x)))
x = x + self.drop_path(self.mlp(self.norm2(x)))
return x
class PatchMerging(nn.Module):
def __init__(self, in_channels, out_channels):
super().__init__()
self.reduction = nn.Linear(4 * in_channels, out_channels, bias=False)
self.norm = nn.LayerNorm(4 * in_channels)
class BasicLayer(nn.Module):
def __init__(self, dim, out_dim, num_blocks, num_heads, mask_shape=None):
super().__init__()
self.blocks = nn.ModuleList([SwinTransformerBlock(dim, num_heads) for _ in range(num_blocks)])
if mask_shape is not None:
for i in range(num_blocks):
if i % 2 != 0:
self.blocks[i].attn_mask = nn.Parameter(torch.zeros(mask_shape), requires_grad=False)
if out_dim is not None:
self.downsample = PatchMerging(dim, out_dim)
else:
self.downsample = None
class PatchEmbed(nn.Module):
def __init__(self):
super().__init__()
self.proj = nn.Conv2d(1, 128, kernel_size=4, stride=4)
self.norm = nn.LayerNorm(128)
class ClapAudioBranch(nn.Module):
"""CLAP Audio Branch with ALL missing modules"""
def __init__(self):
super().__init__()
# Existing modules...
self.spectrogram_extractor = nn.Module()
self.spectrogram_extractor.stft = nn.Module()
self.spectrogram_extractor.stft.conv_real = nn.Conv1d(1, 513, 1024, bias=False)
self.spectrogram_extractor.stft.conv_imag = nn.Conv1d(1, 513, 1024, bias=False)
self.logmel_extractor = nn.Module()
self.logmel_extractor.melW = nn.Parameter(torch.zeros(513, 64))
self.bn0 = nn.BatchNorm1d(64)
# Patch embedding
self.patch_embed = PatchEmbed()
# Layers with proper blocks
self.layers = nn.ModuleList([
BasicLayer(128, 256, 2, 4, mask_shape=(64, 64, 64)),
BasicLayer(256, 512, 2, 8, mask_shape=(16, 64, 64)),
BasicLayer(512, 1024, 12, 16, mask_shape=(4, 64, 64)),
BasicLayer(1024, None, 2, 32)
])
self.norm = nn.LayerNorm(1024)
self.tscam_conv = nn.Conv2d(1024, 527, kernel_size=(2,3), padding=(0,1))
self.head = nn.Linear(527, 527)
# CRITICAL MISSING MODULE:
self.avgpool = nn.AdaptiveAvgPool1d(1) # Global average pooling
class BertEmbeddings(nn.Module):
def __init__(self):
super().__init__()
self.word_embeddings = nn.Embedding(50265, 768)
self.position_embeddings = nn.Embedding(514, 768)
self.token_type_embeddings = nn.Embedding(1, 768)
self.LayerNorm = nn.LayerNorm(768, eps=1e-12)
self.register_buffer("position_ids", torch.arange(514).expand((1, -1)))
class BertSelfAttention(nn.Module):
def __init__(self):
super().__init__()
self.query = nn.Linear(768, 768)
self.key = nn.Linear(768, 768)
self.value = nn.Linear(768, 768)
class BertAttention(nn.Module):
def __init__(self):
super().__init__()
self.self = BertSelfAttention()
self.output = nn.Module()
self.output.dense = nn.Linear(768, 768)
self.output.LayerNorm = nn.LayerNorm(768, eps=1e-12)
class BertIntermediate(nn.Module):
def __init__(self):
super().__init__()
self.dense = nn.Linear(768, 3072)
class BertOutput(nn.Module):
def __init__(self):
super().__init__()
self.dense = nn.Linear(3072, 768)
self.LayerNorm = nn.LayerNorm(768, eps=1e-12)
class BertLayer(nn.Module):
def __init__(self):
super().__init__()
self.attention = BertAttention()
self.intermediate = BertIntermediate()
self.output = BertOutput()
class BertEncoder(nn.Module):
def __init__(self):
super().__init__()
self.layer = nn.ModuleList([BertLayer() for _ in range(12)])
class BertPooler(nn.Module):
def __init__(self):
super().__init__()
self.dense = nn.Linear(768, 768)
class ClapTextBranch(nn.Module):
def __init__(self):
super().__init__()
self.embeddings = BertEmbeddings()
self.encoder = BertEncoder()
self.pooler = BertPooler()
class ClapWrapper(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Module()
self.model.logit_scale_a = nn.Parameter(torch.tensor(0.0))
self.model.logit_scale_t = nn.Parameter(torch.tensor(0.0))
self.model.audio_branch = ClapAudioBranch()
self.model.text_branch = ClapTextBranch()
self.model.text_projection = nn.Sequential(nn.Linear(768, 512), nn.ReLU(), nn.Linear(512, 512))
self.model.audio_projection = nn.Sequential(nn.Linear(1024, 512), nn.ReLU(), nn.Linear(512, 512))
# Transform modules without .sequential nesting to match checkpoint
# Create transform modules with .sequential structure to match AudioSR
self.model.text_transform = nn.Module()
self.model.text_transform.sequential = nn.Sequential(
nn.Linear(512, 512),
nn.ReLU(),
nn.GELU(),
nn.Linear(512, 512)
)
self.model.audio_transform = nn.Module()
self.model.audio_transform.sequential = nn.Sequential(
nn.Linear(512, 512),
nn.ReLU(),
nn.GELU(),
nn.Linear(512, 512)
)
self.mel_transform = nn.Module()
self.mel_transform.spectrogram = nn.Module()
self.mel_transform.spectrogram.window = nn.Parameter(torch.zeros(1024))
self.mel_transform.mel_scale = nn.Module()
self.mel_transform.mel_scale.fb = nn.Parameter(torch.zeros(513, 64))
# --- Custom EMA Handler ---
class CustomLitEma(nn.Module):
def __init__(self, model, decay=0.9999, use_num_updates=True):
super().__init__()
if decay < 0.0 or decay > 1.0:
raise ValueError('Decay must be between 0 and 1')
self.m_name2s_name = {name: name.replace('.', '') for name, p in model.named_parameters()}
self.register_buffer('decay', torch.tensor(decay, dtype=torch.float32))
self.register_buffer('num_updates', torch.tensor(0, dtype=torch.int64) if use_num_updates else torch.tensor(-1, dtype=torch.int64))
for name, p in model.named_parameters():
if p.requires_grad:
s_name = self.m_name2s_name[name]
self.register_buffer(s_name, p.clone().detach().data)
self.collected_params = []
@torch.no_grad()
def forward(self, model):
self.num_updates += 1
for name, p in model.named_parameters():
if p.requires_grad:
s_name = self.m_name2s_name[name]
s_param = self.get_buffer(s_name)
s_param.sub_((1 - self.decay) * (s_param - p.data))
def store(self, params): self.collected_params = [p.clone() for p in params]
def restore(self, params):
for p_old, p_new in zip(params, self.collected_params): p_old.data.copy_(p_new.data)
self.collected_params = []
def copy_to(self, model):
for name, p in model.named_parameters():
if p.requires_grad: p.data.copy_(self.get_buffer(self.m_name2s_name[name]))
# --- Building Blocks for UNet (ResBlock, Upsample, etc.) ---
class TimestepBlock(nn.Module):
@staticmethod
def forward(x, emb): raise NotImplementedError
class TimestepEmbedSequential(nn.Sequential, TimestepBlock):
def forward(self, x, emb, context=None, global_context=None):
for layer in self:
if isinstance(layer, TimestepBlock):
x = layer(x, emb)
elif isinstance(layer, GlobalConditionedSpatialTransformer):
x = layer(x, context, global_context)
elif isinstance(layer, SpatialTransformer):
x = layer(x, context)
else:
x = layer(x)
return x
class Upsample(nn.Module):
def __init__(self, channels, use_conv, dims=2, out_channels=None):
super().__init__()
self.channels, self.out_channels, self.use_conv, self.dims = channels, out_channels or channels, use_conv, dims
if use_conv: self.conv = conv_nd(dims, self.channels, self.out_channels, 3, padding=1)
def forward(self, x):
assert x.shape[1] == self.channels
x = F.interpolate(x, scale_factor=2, mode="nearest")
if self.use_conv: x = self.conv(x)
return x
class Downsample(nn.Module):
def __init__(self, channels, use_conv, dims=2, out_channels=None):
super().__init__()
self.channels, self.out_channels, self.use_conv, self.dims = channels, out_channels or channels, use_conv, dims
stride = 2
if use_conv: self.op = conv_nd(dims, self.channels, self.out_channels, 3, stride=stride, padding=1)
else: self.op = nn.AvgPool2d(kernel_size=stride, stride=stride)
def forward(self, x):
assert x.shape[1] == self.channels
return self.op(x)
class AudioSRResBlock(TimestepBlock):
"""
Exact ResBlock implementation matching AudioSR with h_upd modules
"""
def __init__(self, channels, emb_channels, dropout, out_channels=None, dims=2,
use_checkpoint=False, use_scale_shift_norm=False):
super().__init__()
self.channels = channels
self.emb_channels = emb_channels
self.dropout = dropout
self.out_channels = out_channels or channels
self.use_checkpoint = use_checkpoint
self.use_scale_shift_norm = use_scale_shift_norm
# Standard ResBlock components
self.in_layers = nn.Sequential(
normalization(channels),
nn.SiLU(),
conv_nd(dims, channels, self.out_channels, 3, padding=1)
)
self.emb_layers = nn.Sequential(
nn.SiLU(),
linear(emb_channels, 2 * self.out_channels if use_scale_shift_norm else self.out_channels)
)
self.out_layers = nn.Sequential(
normalization(self.out_channels),
nn.SiLU(),
nn.Dropout(p=dropout),
zero_module(conv_nd(dims, self.out_channels, self.out_channels, 3, padding=1))
)
if self.out_channels == channels:
self.skip_connection = nn.Identity()
else:
self.skip_connection = conv_nd(dims, channels, self.out_channels, 1)
# CRITICAL: Add the missing h_upd module
self.h_upd = nn.Identity() # This matches AudioSR's h_upd structure
def forward(self, x, emb):
"""
Apply the block to a Tensor, conditioned on a timestep embedding.
"""
return checkpoint(self._forward, (x, emb), self.parameters(), self.use_checkpoint)
def _forward(self, x, emb):
h = self.in_layers(x)
emb_out = self.emb_layers(emb).type(h.dtype)
while len(emb_out.shape) < len(h.shape):
emb_out = emb_out[..., None]
if self.use_scale_shift_norm:
out_norm, out_rest = self.out_layers[0], self.out_layers[1:]
scale, shift = torch.chunk(emb_out, 2, dim=1)
h = out_norm(h) * (1 + scale) + shift
h = out_rest(h)
else:
h = h + emb_out
h = self.out_layers(h)
return self.skip_connection(x) + h
# Keep original ResBlock for compatibility
ResBlock = AudioSRResBlock
# --- Main U-Net Model (Corrected Architecture) ---
class UNetModel(nn.Module):
def __init__(self, image_size, in_channels, model_channels, out_channels, num_res_blocks, attention_resolutions, dropout=0, channel_mult=(1, 2, 4, 8), conv_resample=True, dims=2, num_classes=None, use_checkpoint=False, num_heads=-1, num_head_channels=-1, use_scale_shift_norm=False, resblock_updown=False, use_spatial_transformer=False, transformer_depth=1, context_dim=None, use_global_encoder=True, global_context_dim=64,
# --- 1. 新增参数,并提供默认值以保持向后兼容 ---
global_encoder_hidden_dim=128,
global_encoder_heads=4,
global_encoder_layers=2,
**kwargs):
super().__init__()
self.image_size, self.in_channels, self.model_channels, self.out_channels, self.num_res_blocks, self.attention_resolutions, self.dropout, self.channel_mult, self.conv_resample, self.num_classes, self.use_checkpoint, self.num_heads, self.num_head_channels = image_size, in_channels, model_channels, out_channels, num_res_blocks, attention_resolutions, dropout, channel_mult, conv_resample, num_classes, use_checkpoint, num_heads, num_head_channels
# Global Encoder Integration
self.use_global_encoder = use_global_encoder
self.global_context_dim = global_context_dim
if use_global_encoder:
# --- 2. 使用从config传入的参数来实例化 GlobalAudioEncoder ---
self.global_encoder = GlobalAudioEncoder(
input_channels=256,
hidden_dim=global_encoder_hidden_dim,
num_heads=global_encoder_heads,
num_layers=global_encoder_layers,
context_dim=global_context_dim,
max_seq_len=1024
)
time_embed_dim = model_channels * 4
self.time_embed = nn.Sequential(linear(model_channels, time_embed_dim), nn.SiLU(), linear(time_embed_dim, time_embed_dim))
if self.num_classes is not None: self.label_emb = nn.Embedding(num_classes, time_embed_dim)
self.input_blocks = nn.ModuleList([TimestepEmbedSequential(conv_nd(dims, in_channels, model_channels, 3, padding=1))])
self._feature_size, input_block_chans, ch, ds = model_channels, [model_channels], model_channels, 1
for level, mult in enumerate(channel_mult):
for i in range(num_res_blocks):
layers = [ResBlock(ch, time_embed_dim, dropout, out_channels=mult * model_channels, dims=dims, use_checkpoint=use_checkpoint, use_scale_shift_norm=use_scale_shift_norm)]
ch = mult * model_channels
if ds in attention_resolutions:
num_heads = ch // num_head_channels
dim_head = num_head_channels
if self.use_global_encoder:
layers.append(GlobalConditionedSpatialTransformer(ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim, global_context_dim=self.global_context_dim))
else:
layers.append(SpatialTransformer(ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim))
self.input_blocks.append(TimestepEmbedSequential(*layers))
self._feature_size += ch
input_block_chans.append(ch)
if level != len(channel_mult) - 1:
out_ch = ch
self.input_blocks.append(
TimestepEmbedSequential(
Downsample(ch, conv_resample, dims=dims, out_channels=out_ch)
)
)
ch = out_ch
input_block_chans.append(ch)
ds *= 2
self._feature_size += ch
num_heads = ch // num_head_channels
dim_head = num_head_channels
if self.use_global_encoder:
self.middle_block = TimestepEmbedSequential(
ResBlock(ch, time_embed_dim, dropout, dims=dims, use_checkpoint=use_checkpoint, use_scale_shift_norm=use_scale_shift_norm),
GlobalConditionedSpatialTransformer(ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim, global_context_dim=self.global_context_dim),
GlobalConditionedSpatialTransformer(ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim, global_context_dim=self.global_context_dim),
ResBlock(ch, time_embed_dim, dropout, dims=dims, use_checkpoint=use_checkpoint, use_scale_shift_norm=use_scale_shift_norm)
)
else:
self.middle_block = TimestepEmbedSequential(
ResBlock(ch, time_embed_dim, dropout, dims=dims, use_checkpoint=use_checkpoint, use_scale_shift_norm=use_scale_shift_norm),
SpatialTransformer(ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim),
SpatialTransformer(ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim),
ResBlock(ch, time_embed_dim, dropout, dims=dims, use_checkpoint=use_checkpoint, use_scale_shift_norm=use_scale_shift_norm)
)
self._feature_size += ch
self.output_blocks = nn.ModuleList([])
for level, mult in list(enumerate(channel_mult))[::-1]:
for i in range(num_res_blocks + 1):
ich = input_block_chans.pop()
layers = [ResBlock(ch + ich, time_embed_dim, dropout, out_channels=model_channels * mult, dims=dims, use_checkpoint=use_checkpoint, use_scale_shift_norm=use_scale_shift_norm)]
ch = model_channels * mult
if ds in attention_resolutions:
num_heads = ch // num_head_channels
dim_head = num_head_channels
if self.use_global_encoder:
layers.append(GlobalConditionedSpatialTransformer(ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim, global_context_dim=self.global_context_dim))
layers.append(GlobalConditionedSpatialTransformer(ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim, global_context_dim=self.global_context_dim))
else:
layers.append(SpatialTransformer(ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim))
layers.append(SpatialTransformer(ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim))
if level and i == num_res_blocks:
out_ch = ch
layers.append(Upsample(ch, conv_resample, dims=dims, out_channels=out_ch))
ds //= 2
self.output_blocks.append(TimestepEmbedSequential(*layers))
self._feature_size += ch
self.out = nn.Sequential(normalization(ch), nn.SiLU(), zero_module(conv_nd(dims, model_channels, out_channels, 3, padding=1)))
def forward(self, x, timesteps, context=None, y=None, full_audio=None, **kwargs):
hs, t_emb = [], timestep_embedding(timesteps, self.model_channels, repeat_only=False)
emb = self.time_embed(t_emb)
if self.num_classes is not None: emb = emb + self.label_emb(y)
# Extract global context if Global Encoder is enabled
global_context = None
if self.use_global_encoder and full_audio is not None:
global_context_vec, sequence_context = self.global_encoder(full_audio)
global_context = global_context_vec # Use global context for simplicity
h = x
for module in self.input_blocks:
if self.use_global_encoder:
h = module(h, emb, context, global_context)
else:
h = module(h, emb, context)
hs.append(h)
if self.use_global_encoder:
h = self.middle_block(h, emb, context, global_context)
else:
h = self.middle_block(h, emb, context)
for module in self.output_blocks:
if self.use_global_encoder:
h = module(torch.cat([h, hs.pop()], dim=1), emb, context, global_context)
else:
h = module(torch.cat([h, hs.pop()], dim=1), emb, context)
return self.out(h)
# === COMPLETE AUDIOSR-EXACT AUTOENCODER ===
class AudioSRAutoEncoderKL(nn.Module):
def __init__(self, ddconfig, embed_dim, ckpt_path=None, ignore_keys=[]):
super().__init__()
self.encoder = Encoder(**ddconfig)
self.decoder = Decoder(**ddconfig)
# Use EXACT AudioSR vocoder
self.vocoder = AudioSRVocoder()
assert ddconfig["double_z"]
self.quant_conv = torch.nn.Conv2d(2 * ddconfig["z_channels"], 2 * embed_dim, 1)
self.post_quant_conv = torch.nn.Conv2d(embed_dim, ddconfig["z_channels"], 1)
self.embed_dim = embed_dim
def encode(self, x):
return DiagonalGaussianDistribution(self.quant_conv(self.encoder(x)))
def decode(self, z):
return self.decoder(self.post_quant_conv(z))
def decode_to_waveform(self, z):
"""Decode to waveform using exact AudioSR vocoder"""
mel_spec = self.decode(z)
batch_size = mel_spec.shape[0]
if mel_spec.dim() == 4:
mel_spec = mel_spec.squeeze(1)
# Correct reshaping for AudioSR
if mel_spec.shape[1] == 512 and mel_spec.shape[2] == 512:
total_elements = mel_spec.shape[1] * mel_spec.shape[2]
time_frames = total_elements // 256 # n_mels = 256
mel_spec = mel_spec.reshape(batch_size, 256, time_frames)
# CRITICAL: Apply AudioSR's spectral denormalization
from audiosr.utils import spectral_de_normalize_torch
mel_spec_denormalized = spectral_de_normalize_torch(mel_spec)
waveform = self.vocoder(mel_spec_denormalized)
if waveform.dim() == 2:
waveform = waveform.unsqueeze(1)
return waveform
# Keep original AutoencoderKL for compatibility
class AutoencoderKL(nn.Module):
def __init__(self, ddconfig, embed_dim, ckpt_path=None, ignore_keys=[]):
super().__init__()
self.encoder = Encoder(**ddconfig)
self.decoder = Decoder(**ddconfig)
# Initialize AudioSR's HiFi-GAN vocoder
n_mels = ddconfig.get("mel_bins", 256)
self.n_mels = n_mels
# Use EXACT AudioSR vocoder
self.vocoder = AudioSRVocoder()
assert ddconfig["double_z"]
self.quant_conv = torch.nn.Conv2d(2 * ddconfig["z_channels"], 2 * embed_dim, 1)
self.post_quant_conv = torch.nn.Conv2d(embed_dim, ddconfig["z_channels"], 1)
self.embed_dim = embed_dim
def encode(self, x):
return DiagonalGaussianDistribution(self.quant_conv(self.encoder(x)))