-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusiclm.py
1602 lines (1295 loc) · 58.2 KB
/
musiclm.py
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
from functools import wraps, partial
import torch
import torch.nn.functional as F
from torch import nn, einsum
from torchaudio.transforms import Spectrogram, TimeStretch, FrequencyMasking, TimeMasking
import torch.distributed as dist
from distributed import AllGather
from audiolm_pytorch import AudioLM
from audiolm_pytorch.utils import AudioConditionerBase
from x_clip.tokenizer import tokenizer
from vector_quantize_pytorch import ResidualVQ
from einops import rearrange, repeat, reduce, pack, unpack
from einops.layers.torch import Rearrange
from beartype.typing import List, Optional, Tuple
from beartype import beartype
def exists(val):
"""
检查值是否存在(即不为 None)。
Args:
val (Any): 要检查的值。
Returns:
bool: 如果值存在(即不为 None)则返回 True,否则返回 False。
"""
return val is not None
def first(it):
"""
返回可迭代对象中的第一个元素。
Args:
it (Iterable[Any]): 可迭代对象。
Returns:
Any: 可迭代对象中的第一个元素。
"""
return it[0]
def default(val, d):
"""
如果值存在(即不为 None),则返回该值;否则,返回默认值。
Args:
val (Any): 要检查的值。
d (Any): 默认值。
Returns:
Any: 如果 val 存在则返回 val,否则返回 d。
"""
return val if exists(val) else d
def round_down_nearest_multiple(n, divisor):
"""
将一个数向下取整到最接近的指定除数的倍数。
例如:
round_down_nearest_multiple(15, 4) = 12
round_down_nearest_multiple(17, 5) = 15
Args:
n (int): 要取整的数。
divisor (int): 除数。
Returns:
int: 向下取整到最接近的指定除数的倍数。
"""
return n // divisor * divisor
def Sequential(*modules):
"""
创建一个顺序的神经网络模块序列,自动过滤掉任何为 None 的模块。
Args:
*modules (Sequence[nn.Module]): 任意数量的 nn.Module 实例或 None。
Returns:
nn.Sequential: 包含所有非 None 模块的顺序模块序列。
"""
return nn.Sequential(*filter(exists, modules))
def once(fn):
"""
创建一个装饰器,确保被装饰的函数只被调用一次。
Args:
fn (callable): 要装饰的函数。
Returns:
callable: 装饰后的函数。
"""
called = False
@wraps(fn)
def inner(x):
nonlocal called
if called:
return
called = True
return fn(x)
return inner
print_once = once(print)
# tensor functions
def log(t, eps = 1e-20):
"""
计算张量的对数,并避免出现 log(0) 的情况。
Args:
t (torch.Tensor): 输入张量。
eps (float, optional): 一个非常小的数,用于避免 log(0)。默认为 1e-20。
Returns:
torch.Tensor: 对数变换后的张量。
"""
return torch.log(t.clamp(min = eps))
def l2norm(t):
"""
对张量进行 L2 归一化。
Args:
t (torch.Tensor): 输入张量。
Returns:
torch.Tensor: L2 归一化后的张量。
"""
# 对输入张量进行 L2 归一化,维度为最后一个维度
return F.normalize(t, p = 2, dim = -1)
def matrix_diag(t):
"""
从输入矩阵中提取对角线元素。
Args:
t (torch.Tensor): 输入矩阵,张量形状为 (..., i, j)。
Returns:
torch.Tensor: 对角线元素组成的张量,张量形状为 (..., min(i, j))。
"""
device = t.device
# 获取输入张量的最后两个维度大小
i, j = t.shape[-2:]
# 计算对角线元素的数量
num_diag_el = min(i, j)
# 创建行索引范围
i_range = torch.arange(i, device = device)
# 创建列索引范围
j_range = torch.arange(j, device = device)
# 创建对角线掩码,标记对角线元素的位置
diag_mask = rearrange(i_range, 'i -> i 1') == rearrange(j_range, 'j -> 1 j')
# 使用掩码提取对角线元素
diag_el = t.masked_select(diag_mask)
# 重塑对角线元素张量的形状为 (..., min(i, j))
return rearrange(diag_el, '(b d) -> b d', d = num_diag_el)
# 2d sinusoidal positional embedding
# simple vit paper shows it is good enough compared to learned
# 2D 正弦余弦位置编码
# 简单的 ViT 论文表明,与学习得到的位置编码相比,这种方法已经足够好
def posemb_sincos_2d(patches, temperature = 10000, dtype = torch.float32):
"""
生成 2D 正弦余弦位置编码。
Args:
patches (torch.Tensor): 输入的 patches 张量,形状为 (batch_size, height, width, dim)。
temperature (int, optional): 温度参数,用于调整频率。默认为 10000。
dtype (torch.dtype, optional): 返回的张量数据类型。默认为 torch.float32。
Returns:
torch.Tensor: 位置编码张量,形状为 (height, width, dim)。
"""
# 获取 patches 的形状、设备和类型
_, h, w, dim, device, dtype = *patches.shape, patches.device, patches.dtype
# 创建网格坐标 y 和 x,形状分别为 (h, w)
y, x = torch.meshgrid(torch.arange(h, device = device), torch.arange(w, device = device), indexing = 'ij')
# 确保维度 dim 是 4 的倍数,因为位置编码需要分别对 y 和 x 生成正弦和余弦
assert (dim % 4) == 0, 'feature dimension must be multiple of 4 for sincos emb'
# 生成 omega 序列,从 0 到 1
omega = torch.arange(dim // 4, device = device) / (dim // 4 - 1)
# 应用温度参数调整 omega
omega = 1. / (temperature ** omega)
# 对 y 和 x 进行缩放
y = y.flatten()[:, None] * omega[None, :] # 形状 (h*w, dim//4)
x = x.flatten()[:, None] * omega[None, :] # 形状 (h*w, dim//4)
# 生成正弦和余弦位置编码
pe = torch.cat((x.sin(), x.cos(), y.sin(), y.cos()), dim = 1) # 形状 (h*w, dim)
pe = pe.type(dtype) # 转换数据类型
# 重塑为 (h, w, dim)
return rearrange(pe, '(h w) d -> h w d', h = h, w = w)
# biasless layernorm
class LayerNorm(nn.Module):
"""
自定义的 LayerNorm 层,不使用偏置项。
Args:
dim (int): 输入的维度。
scale (bool, optional): 是否使用可学习的缩放因子。默认为 True。
"""
def __init__(self, dim, scale = True):
super().__init__()
# 如果 scale 为 True,则创建一个可学习的缩放因子 gamma;否则,使用固定为 1 的 gamma
self.learned_gamma = nn.Parameter(torch.ones(dim)) if scale else None
# 注册固定的 gamma 和 beta 参数,不作为可训练参数
self.register_buffer('gamma', torch.ones(dim), persistent = False)
self.register_buffer('beta', torch.zeros(dim), persistent = False)
def forward(self, x):
"""
前向传播方法。
Args:
x (torch.Tensor): 输入张量。
Returns:
torch.Tensor: 经过 LayerNorm 处理后的张量。
"""
# 使用 F.layer_norm 进行归一化
# 使用 default 函数选择可学习的 gamma 或固定的 gamma
return F.layer_norm(x, x.shape[-1:], default(self.learned_gamma, self.gamma), self.beta)
# feedforward
class GEGLU(nn.Module):
"""
GEGLU 激活函数模块。
将输入张量沿最后一个维度分成两部分,一部分用于门控(Gating),另一部分用于值(Values)。
然后应用 GeLU 激活函数到门控部分,并将结果与值部分相乘。
Args:
None
Forward Args:
x (torch.Tensor): 输入张量,形状为 (..., dim)。
Returns:
torch.Tensor: 经过 GEGLU 激活后的张量,形状为 (..., dim/2)。
"""
def forward(self, x):
# 将输入张量沿最后一个维度分成两部分
x, gate = x.chunk(2, dim = -1)
# 对门控部分应用 GeLU 激活函数,并将其与值部分相乘
return F.gelu(gate) * x
def FeedForward(dim, mult = 4, dropout = 0.):
"""
前馈网络模块。
由 LayerNorm、线性变换、GEGLU 激活函数、Dropout 和另一个线性变换组成。
Args:
dim (int): 输入和输出的维度。
mult (int, optional): 隐藏层维度的乘数因子。默认为 4。
dropout (float, optional): Dropout 概率。默认为 0。
Returns:
nn.Sequential: 包含前馈网络各层的有序序列。
"""
# 计算隐藏层的维度
dim_hidden = int(dim * mult * 2 / 3)
return nn.Sequential(
LayerNorm(dim), # 第一个 LayerNorm 层
nn.Linear(dim, dim_hidden * 2, bias = False), # 第一个线性变换层,不使用偏置
GEGLU(), # GEGLU 激活函数
nn.Dropout(dropout), # Dropout 层
nn.Linear(dim_hidden, dim, bias = False) # 第二个线性变换层,不使用偏置
)
# attention
class Attention(nn.Module):
"""
自注意力机制模块。
Args:
dim (int): 输入和输出的维度。
causal (bool, optional): 是否使用因果掩码。默认为 False。
dim_head (int, optional): 每个注意力头的维度。默认为 64。
heads (int, optional): 注意力头的数量。默认为 8。
dropout (float, optional): Dropout 概率。默认为 0。
scale (int, optional): 缩放因子,用于调整相似度得分。默认为 8。
"""
def __init__(
self,
dim,
causal = False,
dim_head = 64,
heads = 8,
dropout = 0.,
scale = 8
):
super().__init__()
# 注意力头的数量
self.heads = heads
# 缩放因子
self.scale = scale
# 是否使用因果掩码
self.causal = causal
# 内部维度
inner_dim = dim_head * heads
# LayerNorm 层
self.norm = LayerNorm(dim)
# Dropout 层,用于注意力得分
self.attn_dropout = nn.Dropout(dropout)
# 线性变换,用于生成查询(q)
self.to_q = nn.Linear(dim, inner_dim, bias = False)
# 线性变换,用于生成键(k)和值(v)
self.to_kv = nn.Linear(dim, inner_dim * 2, bias = False)
# 查询的缩放因子参数
self.q_scale = nn.Parameter(torch.ones(dim_head))
# 键的缩放因子参数
self.k_scale = nn.Parameter(torch.ones(dim_head))
# 输出线性变换和 Dropout 层
self.to_out = nn.Sequential(
nn.Linear(inner_dim, dim, bias = False),
nn.Dropout(dropout)
)
def forward(
self,
x, # 输入张量,形状为 (batch_size, sequence_length, dim)
rel_pos_bias = None, # 相对位置偏置,可选
mask = None # 注意力掩码,可选
):
# 获取输入张量的批大小、序列长度和设备信息
b, n, _, device = *x.shape, x.device
# prenorm
# 前置 LayerNorm
x = self.norm(x)
# project for queries, keys, values
# 线性变换生成查询(q)、键(k)和值(v)
q, k, v = self.to_q(x), *self.to_kv(x).chunk(2, dim = -1)
# split for multi-headed attention
# 多头注意力:将查询、键和值重塑为 (batch_size, heads, sequence_length, dim_head)
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = self.heads), (q, k, v))
# qk rmsnorm, technique circulating within brain used to stabilize a 22B parameter vision model training
# qk RMSNorm,稳定训练大模型的技术
# 对查询和键进行 L2 归一化
q, k = map(l2norm, (q, k))
# 对查询进行缩放
q = q * self.q_scale
# 对键进行缩放
k = k * self.k_scale
# similarities
# 计算相似度得分
# 计算 q 和 k 的点积,并乘以缩放因子
sim = einsum('b h i d, b h j d -> b h i j', q, k) * self.scale
if exists(rel_pos_bias):
# 如果存在相对位置偏置,则将其加到相似度得分上
sim = sim + rel_pos_bias
if exists(mask):
# 如果提供了掩码,则使用掩码填充相似度得分
mask = rearrange(mask, 'b j -> b 1 1 j') # 重塑掩码形状
sim = sim.masked_fill(~mask, -torch.finfo(sim.dtype).max) # 使用掩码填充
# 如果使用因果掩码,则应用因果掩码
if self.causal:
# 获取相似度得分的最后两个维度
i, j = sim.shape[-2:]
# 创建上三角掩码
causal_mask = torch.ones((i, j), dtype = torch.bool, device = x.device).triu(j - i + 1)
# 使用因果掩码填充
sim = sim.masked_fill(causal_mask, -torch.finfo(sim.dtype).max)
# attention
# 计算注意力权重
# 对相似度得分进行 softmax 归一化
attn = sim.softmax(dim = -1)
# 应用 Dropout
attn = self.attn_dropout(attn)
# aggregate
# 计算注意力权重与值的乘积
out = einsum('b h i j, b h j d -> b h i d', attn, v)
# merge heads
# 合并多头
# 将多头张量重塑为 (batch_size, sequence_length, heads * dim_head)
out = rearrange(out, 'b h n d -> b n (h d)')
# 通过输出线性变换层
return self.to_out(out)
# transformer
class Transformer(nn.Module):
"""
Transformer 编码器模块,由多个自注意力层和前馈层组成。
Args:
dim (int): 输入和输出的维度。
depth (int): Transformer 层的数量。
dim_head (int, optional): 每个注意力头的维度。默认为 64。
heads (int, optional): 注意力头的数量。默认为 8。
attn_dropout (float, optional): 注意力层的 Dropout 概率。默认为 0。
ff_mult (int, optional): 前馈网络中隐藏层维度的乘数因子。默认为 4。
ff_dropout (float, optional): 前馈层的 Dropout 概率。默认为 0。
"""
def __init__(
self,
dim,
depth,
dim_head = 64,
heads = 8,
attn_dropout = 0.,
ff_mult = 4,
ff_dropout = 0.
):
super().__init__()
# 创建一个空的有序模块列表,用于存储 Transformer 层的子模块
self.layers = nn.ModuleList([])
# 根据指定的深度,添加 Transformer 层
for _ in range(depth):
self.layers.append(nn.ModuleList([
Attention(dim = dim, dim_head = dim_head, heads = heads, dropout = attn_dropout), # 自注意力层
FeedForward(dim = dim, mult = ff_mult, dropout = ff_dropout), # 前馈层
]))
def forward(
self,
x, # 输入张量,形状为 (batch_size, sequence_length, dim)
rel_pos_bias = None, # 相对位置偏置,可选
mask = None, # 注意力掩码,可选
return_all_layers = False # 是否返回所有层的输出,默认为 False
):
# 初始化一个空列表,用于存储每一层的输出
layers = []
for attn, ff in self.layers:
# 自注意力层
# 自注意力层的输出与输入相加
x = attn(x, rel_pos_bias = rel_pos_bias, mask = mask) + x
# 前馈层
# 前馈层的输出与输入相加
x = ff(x) + x
# 将当前层的输出添加到列表中
layers.append(x)
if not return_all_layers:
# 如果不需要返回所有层的输出,则返回最后一层的输出
return x
# 如果需要返回所有层的输出,则返回最后一层的输出和所有层的输出张量
return x, torch.stack(layers[:-1])
# contrastive losses
class SoftmaxContrastiveLearning(nn.Module):
"""
Softmax 对比学习模块。
Args:
layers (int, optional): 对比学习层的数量。默认为 1。
decoupled_contrastive_learning (bool, optional): 是否使用解耦对比学习。默认为 False。
init_temp (float, optional): 初始温度参数。默认为 10。
"""
def __init__(
self,
*,
layers = 1,
decoupled_contrastive_learning = False,
init_temp = 10
):
super().__init__()
# 初始化温度参数,使用 log 缩放
self.temperatures = nn.Parameter(torch.ones(layers, 1, 1) * math.log(init_temp))
# 是否使用解耦对比学习
self.decoupled_contrastive_learning = decoupled_contrastive_learning
# 初始化 AllGather 模块,用于分布式训练
self.all_gather = AllGather(dim = 2)
@property
def device(self):
"""
获取模型所在的设备。
Returns:
torch.device: 模型所在的设备。
"""
return next(self.parameters()).device
def forward(self, audio_latents, text_latents):
"""
前向传播方法,计算对比学习损失。
Args:
audio_latents (torch.Tensor): 音频的潜在表示,形状为 (batch_size, latent_dim)。
text_latents (torch.Tensor): 文本的潜在表示,形状为 (batch_size, latent_dim)。
Returns:
torch.Tensor: 对比学习损失。
"""
if audio_latents.ndim == 2:
# 如果音频潜在表示的维度为 2,则添加一个维度,使其形状为 (1, batch_size, latent_dim)
audio_latents = rearrange(audio_latents, '... -> 1 ...')
if text_latents.ndim == 2:
# 如果文本潜在表示的维度为 2,则添加一个维度,使其形状为 (1, batch_size, latent_dim)
text_latents = rearrange(text_latents, '... -> 1 ...')
# 获取批大小
batch = audio_latents.shape[1]
if self.all_gather.is_distributed:
# 如果在分布式环境中,则将音频和文本潜在表示堆叠起来
latents = torch.stack((audio_latents, text_latents))
# 使用 AllGather 模块收集所有进程的潜在表示
latents, _ = self.all_gather(latents)
audio_latents, text_latents = latents
# 计算音频和文本潜在表示之间的相似度得分
sims = einsum('l i d, l j d -> l i j', audio_latents, text_latents)
# 将相似度得分乘以温度参数的指数
sims = sims * self.temperatures.exp()
# 计算指数相似度得分
cosine_sims_exp = sims.exp()
# 计算对角线元素,即正样本的指数相似度得分
numerator = matrix_diag(cosine_sims_exp)
if self.decoupled_contrastive_learning:
# 如果使用解耦对比学习,则将自身相似度得分掩码掉
eye = torch.eye(batch, device = self.device, dtype = torch.bool)
cosine_sims_exp = cosine_sims_exp.masked_fill(eye, 0.)
# 计算分母中的 i 和 j 维度上的和
denominator_i = reduce(cosine_sims_exp, 'l i j -> l i', 'sum')
denominator_j = reduce(cosine_sims_exp, 'l i j -> l j', 'sum')
# 计算对比学习损失
contrastive_loss = -log(numerator) + 0.5 * (log(denominator_i) + log(denominator_j))
# 对每个样本的损失进行平均
contrastive_loss = reduce(contrastive_loss, 'l n -> l', 'mean')
# 返回所有层的对比学习损失之和
return contrastive_loss.sum()
class SigmoidContrastiveLearning(nn.Module):
""" https://arxiv.org/abs/2303.15343 """
"""
Sigmoid 对比学习模块。
Args:
layers (int, optional): 对比学习层的数量。默认为 1。
init_temp (float, optional): 初始温度参数。默认为 10。
init_bias (float, optional): 初始偏置参数。默认为 -10。
"""
def __init__(
self,
*,
layers = 1,
init_temp = 10,
init_bias = -10
):
super().__init__()
# 初始化温度参数,使用 log 缩放
self.temperatures = nn.Parameter(torch.ones(layers, 1, 1) * math.log(init_temp))
# 初始化偏置参数
self.bias = nn.Parameter(torch.ones(layers, 1, 1) * init_bias)
# 初始化 AllGather 模块,用于分布式训练
self.all_gather = AllGather(dim = 1, all_reduce_grads = True)
@property
def device(self):
"""
获取模型所在的设备。
Returns:
torch.device: 模型所在的设备。
"""
return next(self.parameters()).device
def forward(self, audio_latents, text_latents):
"""
前向传播方法,计算 Sigmoid 对比学习损失。
Args:
audio_latents (torch.Tensor): 音频的潜在表示,形状为 (batch_size, latent_dim)。
text_latents (torch.Tensor): 文本的潜在表示,形状为 (batch_size, latent_dim)。
Returns:
torch.Tensor: Sigmoid 对比学习损失。
"""
device = self.device
if audio_latents.ndim == 2:
# 如果音频潜在表示的维度为 2,则添加一个维度,使其形状为 (1, batch_size, latent_dim)
audio_latents = rearrange(audio_latents, '... -> 1 ...')
if text_latents.ndim == 2:
# 如果文本潜在表示的维度为 2,则添加一个维度,使其形状为 (1, batch_size, latent_dim)
text_latents = rearrange(text_latents, '... -> 1 ...')
# 使用 AllGather 模块收集所有进程的文本潜在表示
text_latents, rank_sizes = self.all_gather(text_latents)
# 获取文本潜在表示的批大小
n = text_latents.shape[1]
# 计算音频和文本潜在表示之间的相似度得分
sims = einsum('l i d, l j d -> l i j', audio_latents, text_latents)
# 将相似度得分乘以温度参数的指数,并加上偏置
sims = sims * self.temperatures.exp() + self.bias
# 创建标签矩阵,对角线为 1,其余为 0
labels = torch.eye(n, device = device)
if exists(rank_sizes):
# 如果存在 rank_sizes,则根据进程数量分割标签矩阵
labels_by_ranks = labels.split(rank_sizes.tolist(), dim = 0)
# 获取当前进程的标签矩阵
labels = labels_by_ranks[dist.get_rank()]
# 将标签矩阵转换为与相似度得分相同的形状,并进行缩放
labels = 2 * rearrange(labels, 'i j -> 1 i j') - torch.ones_like(sims)
# 计算 Sigmoid 对比学习损失
return -F.logsigmoid(labels * sims).sum() / n
# Audio Spectrogram Transformer - https://arxiv.org/abs/2104.01778
def pair(t):
"""
如果输入 t 不是元组,则返回 (t, t);否则返回 t。
Args:
t (Any): 输入值。
Returns:
Tuple[Any, Any]: 返回的元组。
"""
return (t, t) if not isinstance(t, tuple) else t
class AudioSpectrogramTransformer(nn.Module):
"""
AST-音频频谱Transformer模型。
Args:
dim (int): 输入和输出的维度。
depth (int): Transformer 层的数量。
patch_size (int or tuple, optional): 图像块的大小。默认为 16。
dim_head (int, optional): 每个注意力头的维度。默认为 64。
heads (int, optional): 注意力头的数量。默认为 8。
attn_dropout (float, optional): 注意力层的 Dropout 概率。默认为 0。
ff_mult (int, optional): 前馈网络中隐藏层维度的乘数因子。默认为 4。
ff_dropout (float, optional): 前馈层的 Dropout 概率。默认为 0。
accept_spec (bool, optional): 是否接受频谱作为输入。默认为 False。
accept_spec_time_first (bool, optional): 频谱的时间维度是否在第一维。默认为 True。
spec_n_fft (int, optional): 快速傅里叶变换的窗口大小。默认为 128。
spec_power (float, optional): 频谱的幂。默认为 2。
spec_win_length (int, optional): 窗口长度。默认为 24。
spec_hop_length (int, optional): 跳跃长度。默认为 None。
spec_pad (int, optional): 填充长度。默认为 0。
spec_center (bool, optional): 是否以中心为中心进行填充。默认为 True。
spec_pad_mode (str, optional): 填充模式。默认为 'reflect'。
spec_aug_stretch_factor (float, optional): 时拉伸因子。默认为 0.8。
spec_aug_freq_mask (int, optional): 频率掩码参数。默认为 80。
spec_aug_time_mask (int, optional): 时间掩码参数。默认为 80。
patch_dropout_prob (float, optional): 图像块 Dropout 概率。默认为 0.25。
"""
def __init__(
self,
dim,
depth,
patch_size = 16,
dim_head = 64,
heads = 8,
attn_dropout = 0.,
ff_mult = 4,
ff_dropout = 0.,
accept_spec = False,
accept_spec_time_first = True,
spec_n_fft = 128,
spec_power = 2,
spec_win_length = 24,
spec_hop_length = None,
spec_pad = 0,
spec_center = True,
spec_pad_mode = 'reflect',
spec_aug_stretch_factor = 0.8,
spec_aug_freq_mask = 80,
spec_aug_time_mask = 80,
patch_dropout_prob = 0.25
):
super().__init__()
# 输入和输出的维度
self.dim = dim
# Transformer 层的数量
self.depth = depth
# 图像块的大小
self.patch_size = pair(patch_size)
# 图像块的输入维度
patch_input_dim = self.patch_size[0] * self.patch_size[1]
# 将输入图像转换为图像块
self.to_patch_tokens = Sequential(
Rearrange('b (h p1) (w p2) -> b h w (p1 p2)', p1 = self.patch_size[0], p2 = self.patch_size[1]), # 重塑张量形状
nn.LayerNorm(patch_input_dim), # LayerNorm 层
nn.Linear(patch_input_dim, dim), # 线性变换层
nn.LayerNorm(dim) # LayerNorm 层
)
# 是否接受频谱作为输入
self.accept_spec = accept_spec
# 频谱的时间维度是否在第一维
self.accept_spec_time_first = accept_spec_time_first
# 定义频谱转换器
self.spec = Spectrogram(
n_fft = spec_n_fft, # 快速傅里叶变换的窗口大小
power = spec_power, # 频谱的幂
win_length = spec_win_length, # 窗口长度
hop_length = spec_hop_length, # 跳跃长度
pad = spec_pad, # 填充长度
center = spec_center, # 是否以中心为中心进行填充
pad_mode = spec_pad_mode # 填充模式
)
# SpecAugment - seems to be widely used in audio field https://arxiv.org/abs/1904.08779
# SpecAugment 增强方法
self.aug = torch.nn.Sequential(
TimeStretch(spec_aug_stretch_factor, fixed_rate = True), # 时拉伸增强
FrequencyMasking(freq_mask_param = spec_aug_freq_mask), # 频率掩码增强
TimeMasking(time_mask_param = spec_aug_time_mask), # 时间掩码增强
)
# Transformer 编码器
self.transformer = Transformer(
dim = dim, # 输入和输出的维度
depth = depth, # Transformer 层的数量
dim_head = dim_head, # 每个注意力头的维度
heads = heads, # 注意力头的数量
attn_dropout = attn_dropout, # 注意力层的 Dropout 概率
ff_mult = ff_mult, # 前馈网络中隐藏层维度的乘数因子
ff_dropout = ff_dropout # 前馈层的 Dropout 概率
)
# LayerNorm 层
self.norm = LayerNorm(dim)
# patch dropout
# 图像块 Dropout 概率
self.patch_dropout_prob = patch_dropout_prob
# 2d dynamic positional bias
# 2D 动态位置偏置
# MLP 隐藏层的维度
mlp_hidden_dim = dim // 4
self.dynamic_pos_bias_mlp = nn.Sequential(
nn.Linear(2, mlp_hidden_dim), # 线性变换层
nn.SiLU(), # SiLU 激活函数
nn.Linear(mlp_hidden_dim, mlp_hidden_dim), # 线性变换层
nn.SiLU(), # SiLU 激活函数
nn.Linear(mlp_hidden_dim, heads), # 线性变换层
Rearrange('... i j h -> ... h i j') # 重塑张量形状
)
def forward(
self,
x, # 输入张量
force_no_patch_dropout = False, # 是否强制不进行图像块 Dropout
return_all_layers = False # 是否返回所有 Transformer 层的输出
):
# 获取批大小和设备信息
batch, device = x.shape[0], x.device
# 确保输入张量的维度是否符合要求
# 如果接受频谱输入,则维度应为 3;否则,维度应为 2
assert (self.accept_spec and x.ndim == 3) or (not self.accept_spec and x.ndim == 2)
if self.accept_spec and self.accept_spec_time_first:
# 如果接受频谱输入且时间维度在第一维,则重塑张量形状
x = rearrange(x, 'b t f -> b f t')
if not self.accept_spec:
# 如果不接受频谱输入,则应用频谱转换
x = self.spec(x)
if self.training:
# 如果在训练模式下,则应用数据增强
x = self.aug(x)
# automatically crop if audio does not yield a 2d spectrogram that is divisible by patch sizes
# 如果音频频谱不能被图像块大小整除,则自动裁剪
# 获取频谱的高度和宽度
height, width = x.shape[-2:]
# 获取图像块的高度和宽度
patch_height, patch_width = self.patch_size
# 计算频谱的高度和宽度向下取整到最接近的图像块大小的倍数
rounded_height, rounded_width = map(lambda args: round_down_nearest_multiple(*args), ((height, patch_height), (width, patch_width)))
if (height, width) != (rounded_height, rounded_width): # just keep printing to be annoying until it is fixed
# 如果裁剪后的尺寸与原始尺寸不同,则打印警告信息
# 这里使用 print_once 确保只打印一次
print_once(f'spectrogram yielded shape of {(height, width)}, but had to be cropped to {(rounded_height, rounded_width)} to be patchified for transformer')
# 裁剪频谱到计算后的尺寸
x = x[..., :rounded_height, :rounded_width]
# to patches
# 将输入转换为图像块
x = self.to_patch_tokens(x)
# get number of patches along height and width
# 获取沿高度和宽度方向的图像块数量
_, num_patch_height, num_patch_width, _ = x.shape
# get 2d relative positions
# 生成 2D 相对位置网格
grid = torch.stack(torch.meshgrid(
torch.arange(num_patch_height, device = device),
torch.arange(num_patch_width, device = device)
, indexing = 'ij'), dim = -1)
# 重塑网格形状
grid = rearrange(grid, '... c -> (...) c')
# 2d sinusoidal positional embedding
# 生成 2D 正弦余弦位置编码并添加到输入张量中
x = x + posemb_sincos_2d(x)
# 重塑张量形状
x = rearrange(x, 'b ... c -> b (...) c')
# patch dropout
# 图像块 Dropout
if self.training and self.patch_dropout_prob > 0. and not force_no_patch_dropout:
n, device = x.shape[1], x.device
# 生成批索引
batch_indices = torch.arange(batch, device = device)
# 重塑批索引形状
batch_indices = rearrange(batch_indices, '... -> ... 1')
# 计算要保留的图像块数量
num_patches_keep = max(1, int(n * (1 - self.patch_dropout_prob)))
# 随机选择要保留的图像块索引
patch_indices_keep = torch.randn(batch, n, device = device).topk(num_patches_keep, dim = -1).indices
# 根据索引选择图像块
x = x[batch_indices, patch_indices_keep]
# 重复网格以匹配批大小
grid = repeat(grid, '... -> b ...', b = batch)
# 根据索引选择网格
grid = grid[batch_indices, patch_indices_keep]
# 2d relative positional bias
# 计算 2D 相对位置偏置
# 计算相对距离
rel_dist = rearrange(grid, '... i c -> ... i 1 c') - rearrange(grid, '... j c -> ... 1 j c')
# 计算相对位置偏置
rel_pos_bias = self.dynamic_pos_bias_mlp(rel_dist.float())
# attention, what else
# 应用自注意力机制
x, all_layers = self.transformer(x, rel_pos_bias = rel_pos_bias, return_all_layers = True)
# final global average and norm (most recent papers show this is superior to CLS token)
# 对输出进行全局平均池化和归一化
# 最近的研究表明,这种方法比使用 CLS 标记更优
x = reduce(x, 'b n d -> b d', 'mean')
# 应用归一化
out = self.norm(x)
if not return_all_layers:
# 如果不需要返回所有层的输出,则返回最终输出
return out
# 如果需要返回所有层的输出,则返回最终输出和所有层的输出列表
return out, all_layers
# text transformer
class TextTransformer(nn.Module):
"""
基于 Transformer 的文本编码器模型。
Args:
dim (int): 输入和输出的维度。
depth (int): Transformer 层的数量。
num_tokens (int, optional): 词汇表的大小。默认为 tokenizer.vocab_size。
max_seq_len (int, optional): 最大序列长度。默认为 256。
dim_head (int, optional): 每个注意力头的维度。默认为 64。
heads (int, optional): 注意力头的数量。默认为 8。
attn_dropout (float, optional): 注意力层的 Dropout 概率。默认为 0。
ff_dropout (float, optional): 前馈层的 Dropout 概率。默认为 0。
ff_mult (int, optional): 前馈网络中隐藏层维度的乘数因子。默认为 4。
pad_id (int, optional): 填充标记的 ID。默认为 0。
"""
@beartype
def __init__(
self,
dim,
depth,
num_tokens = tokenizer.vocab_size,
max_seq_len = 256,
dim_head = 64,
heads = 8,
attn_dropout = 0.,
ff_dropout = 0.,
ff_mult = 4,
pad_id = 0
):
super().__init__()
# 输入和输出的维度
self.dim = dim
# 词嵌入层
self.token_emb = nn.Embedding(num_tokens, dim)
# 位置嵌入层
self.pos_emb = nn.Embedding(max_seq_len, dim)
# Transformer 层的数量
self.depth = depth
# 最大序列长度
self.max_seq_len = max_seq_len
# CLS 标记参数
self.cls_token = nn.Parameter(torch.randn(dim))
# Transformer 编码器
self.transformer = Transformer(
dim = dim,
depth = depth,
dim_head = dim_head,
heads = heads,
attn_dropout = attn_dropout,
ff_dropout = ff_dropout,
ff_mult = ff_mult
)
# 填充标记的 ID
self.pad_id = pad_id
# LayerNorm 层
self.norm = LayerNorm(dim)
@property
def device(self):
"""
获取模型所在的设备。
Returns:
torch.device: 模型所在的设备。