深度学习模型通常堆叠大量结构和功能相同的结构,虽然有效,但会导致参数数量大幅增加,给实际应用带来了挑战。为了缓解这个问题,LORS
(低秩残差结构)允许堆叠模块共享大部分参数,每个模块仅需要少量的唯一参数即可匹配甚至超过全量参数的性能。实验结果表明,LORS
减少解码器 70% 的参数后仍可达到与原始模型相当甚至更好的性能
来源:晓飞的算法工程笔记 公众号
论文: LORS: Low-rank Residual Structure for Parameter-Efficient Network Stacking
Introduction
在当前大模型繁荣的时代,一个普遍的问题是参数量大幅增加,这给训练、推理和部署带来了挑战。目前有各种方法来减少模型中的参数数量,例如知识蒸馏,将大型模型压缩为较小的模型,同时试图保持其性能,但仍可能导致模型容量的下降;剪枝,从模型中删除冗余参数,但会影响模型的稳定性;量化,降低模型参数的数值精度,降低存储和计算量,但可能会导致模型精度损失;参数共享,通过在不同层之间共享参数来减少参数数量,但可能会限制模型的表达能力。
与上述方法不同,论文观察到一个导致参数数量庞大的重要事实:层堆叠在神经网络中的广泛使用。层堆叠是指那些具有相同架构并执行相同或相似功能的模块,但由于随机初始化和训练更新而具有不同的参数。堆叠的例子可以在许多著名的神经网络中找到,比如经典的ResNet
模型和Transformers
。特别是,Transformers
严重依赖堆栈结构,并且通常在编码器和解码器中采用完全相同的多层堆栈。现在它已成为计算机视觉、自然语言处理等领域许多优秀模型不可或缺的组成部分。
尽管层堆叠对于增强模型容量非常有效,但也会导致参数数量的急剧增加。例如,GPT-3
使用 1750 亿个参数,由 96 个堆叠的Transformer
层组成。如何才能享受堆栈带来的好处,同时减少所需的参数数量?论文注意到堆叠的解码器具有相同的结构和相似的功能,这表明它们的参数之间应该存在一些共性。然而,由于它们处理不同的输入和输出分布,因此它们的参数也必须有独特的方面。因此,一个自然的想法是:也许可以用共享参数来表示共享方面,同时允许每个堆叠模块仅保留捕获其独特特征的参数,从而减少总体参数使用。
基于上述考虑,论文建议将堆叠模块的参数分解为两部分:代表共性的共享参数和捕获特定特征的私有参数。共享参数可供所有模块使用并共同训练,而私有参数则由每个模块单独拥有,在保持模型性能的同时减少参数量。为了实现这一目标,受LoRA
方法的启发,论文引入了低秩残差结构 (LORS
) 的概念,本质上是将私有参数添加到共享参数中,就像残差连接将残差信息添加到特征中一样。
为了验证论文的想法,选择AdaMixer
(一个强大的基于查询的对象检测器)作为实验对象。其堆叠的解码器中包含大量自适应和静态参数,是展示LORS
有效性的理想候选者。自适应参数和静态参数的区别在于是否随着不同的输入而变化,而论文的目标是证明LORS
可以有效减少两类参数的总体使用,同时保持模型的性能。对此检测器进行的广泛实验表明,LORS
成功地减少了AdaMixer
解码器中高达 70% 的参数,同时能够实现与其普通版本相当甚至更优越的性能。
总之,论文的贡献可以总结为:
- 论文提出了用于堆叠网络的新颖低秩残差结构
LORS
,与普通结构相比,在大幅减少参数数量的同时保持甚至提高性能。
- 论文引入有效的方法来减少堆叠结构中的静态参数和自适应生成参数,这使得论文提出的
LORS
更加通用。
- 论文的方法有潜力作为大型模型的基本网络结构之一,这些模型受到堆叠网络导致的参数过多问题的影响很大。
LORS
能使参数更加高效,从而在实际应用中更容易实现。
Approach
Preliminary
低秩适应(LoRA
)技术是一种新颖的方法,使大型预训练语言模型能够适应特定任务。LoRA
的关键思想是引入一个低秩参数矩阵,该矩阵能够捕获任务相关的知识,同时保持原始预训练参数固定。
从数学角度来看,给定一个预训练的参数矩阵 W∈Rd×h" role="presentation">W∈Rd×h,LoRA
使用低秩矩阵 B∈Rd×r" role="presentation">B∈Rd×r 和投影矩阵 A∈Rr×h" role="presentation">A∈Rr×h 来适应 W" role="presentation">W ,其中秩 r≪d,h" role="presentation">r≪d,h。适应的参数矩阵由以下计算:
W+△W=W+BA(1)" role="presentation">W+△W=W+BA(1)
其中BA" role="presentation">BA 用于捕获特定于任务的知识。
LoRA
的主要优势在于显着减少需要微调的参数量,从而降低计算成本以及内存需求。在某些情况下,即使个位数值的秩 r" role="presentation">r 也足以将模型微调到所需状态,比直接训练 W" role="presentation">W 中的参数的开销少数十倍。此外,通过固定原始参数,LoRA
避免了灾难性遗忘,这是微调大型模型时的常见问题。
-
Query-based object detection
在对象检测领域,基于查询的检测器建立了一种新的范例,利用一组可学习的查询向量与图像特征图进行交互:
Qupdated=Atention(Q,K(V))(2)" role="presentation">Qupdated=Atention(Q,K(V))(2)
其中 Q" role="presentation">Q、K" role="presentation">K 和 V" role="presentation">V 表示查询、键和值。可学习查询 Q" role="presentation">Q 最终用于预测对象类和边界框,而 K" role="presentation">K 和 V" role="presentation">V 通常为编码的图像特征。经过连续的解码层,Q" role="presentation">Q 不断地与 K" role="presentation">K 和 V" role="presentation">V 交互来细化是一种常见的做法,而这些层通常由结构相同的解码器组成。
AdaMixer
是一种基于查询的检测器,添加自适应通道混合(ACM
)和自适应空间混合(ASM
)方法,大大增强了性能。
给定一个采样特征 x∈ RPin×C" role="presentation">x∈ RPin×C,其中 C =dfeat/g" role="presentation">C =dfeat/g,g" role="presentation">g 为采样组数。采样特征通过组采样操作获得的,该操作将每个多尺度特征的空间通道 dfeat" role="presentation">dfeat 划分为 g" role="presentation">g 组,然后对每个组内特征进行单独3D采样操作,得到的多组采样特征分别进行后续的ACM
和ASM
操作。
首先对采样特征执行ACM
(自适应通道混合)操作,根据对象查询 q" role="presentation">q 生成的自适应权重在通道维度转换特征 x" role="presentation">x,增强通道语义:
Mc=Linear(q)∈RC×C(3)ACM(x)=ReLU(LayerNorm(xMc))(4)" role="presentation">Mc=Linear(q)∈RC×C(3)ACM(x)=ReLU(LayerNorm(xMc))(4)
随后对通道增强的采样特征执行ASM
(自适应空间混合)操作,通过对空间维度应用自适应变换,使得对象查询 q" role="presentation">q 能够适应采样特征的空间结构:
Ms=Linear(q)∈RPin×Pout(5)ASM(x)=ReLU(LayerNorm(xTMs))(6)" role="presentation">Ms=Linear(q)∈RPin×Pout(5)ASM(x)=ReLU(LayerNorm(xTMs))(6)
ACM
和ASM
都为每个采样组训练独立的参数,最后整合多组输出将形状为 Rg×C×Pout" role="presentation">Rg×C×Pout 的合并输出展平并通过线性层 Loutput" role="presentation">Loutput 输出转换为 dq" role="presentation">dq维度,添加到原对象查询中。
与解码器的其他操作相比,ACM
、ASM
和输出线性变换 Loutput" role="presentation">Loutput 拥有更多的参数,是模型参数量的主要贡献者。因此,论文选择它们作为目标组件来验证LORS
方法在参数减少方面的有效性。
LORS
的完整计算过程如图 1 所示,分为两种类型:自适应计算和静态计算。其中,“自适应”表示变换矩阵是否依赖于对象查询。
-
Static Low Rank Residual Structure (LORST" role="presentation">LORST)
假设单个模块包含 N" role="presentation">N 个具有相同架构的堆叠层,并且 Wi∈Rd×h" role="presentation">Wi∈Rd×h 是第 i" role="presentation">i 层的参数矩阵,则有:
Wi=Wshared+Wiprivate(7)" role="presentation">Wi=Wshared+Wprivatei(7)
Wshared" role="presentation">Wshared 为所有堆叠层的共享参数,Wiprivate" role="presentation">Wprivatei 为第 i" role="presentation">i 层的私有参数,其计算如下:
Wiprivate=∑k=1KBikAik(8)" role="presentation">Wprivatei=K∑k=1BikAik(8)
Bik∈Rd×r" role="presentation">Bik∈Rd×r,Aik∈Rr×h" role="presentation">Aik∈Rr×h,其中秩 r≪d" role="presentation">r≪d。K" role="presentation">K 为用于计算Wiprivate" role="presentation">Wprivatei 的参数组的数量。
LORST" role="presentation">LORST 计算 Wiprivate" role="presentation">Wprivatei 的伪代码如图 2 所示。
-
Adaptive Low Rank Residual Structure (LORSA" role="presentation">LORSA)
定义 W^i∈Rd×h" role="presentation">^Wi∈Rd×h 为第 i" role="presentation">i 个堆叠层中自适应生成参数,其计算为:
W^i=W^shared+W^iprivate(9)" role="presentation">^Wi=^Wshared+^Wprivatei(9)
其中跨层共享参数 W^shared∈Rd×h" role="presentation">^Wshared∈Rd×h 和层私有参数 W^iprivate∈Rd×h" role="presentation">^Wprivatei∈Rd×h都是基于查询 q" role="presentation">q 计算得到的:
W^ishared=Linear(q)∈Rd×h(10)W^iprivate=∑k=1KB^ikE^ikA^ik (11)E^ik=Linear(q)∈Rr×r(12)" role="presentation">^Wsharedi=Linear(q)∈Rd×h(10)^Wprivatei=∑Kk=1^Bik^Eik^Aik (11)^Eik=Linear(q)∈Rr×r(12)
其中 B^ik ∈ Rd×r" role="presentation">^Bik ∈ Rd×r,A^ik∈Rr×h" role="presentation">^Aik∈Rr×h,秩 r≪d,h" role="presentation">r≪d,h。
LORSA" role="presentation">LORSA 计算 W^iPrivate" role="presentation">^WPrivatei 的伪代码如图 3 所示。
Applying LORS to AdaMixer’s Decoders
将LORS
应用到AdaMixer
的每个解码器的ACM
、ASM
和 Loutput" role="presentation">Loutput 的线性变换的参数中。
AdaMixer
中运行的LORS
的整体流程如图 4 所示。对于每组采样点,LORSA" role="presentation">LORSA 用于减少ACM
的 Mc" role="presentation">Mc(映射 Rdq" role="presentation">Rdq 到 RC×C" role="presentation">RC×C)和ASM
的 Ms" role="presentation">Ms 的参数(映射 Rdq" role="presentation">Rdq 到 RPin×Pout" role="presentation">RPin×Pout,而 LORST" role="presentation">LORST 则用于最小化 Loutput" role="presentation">Loutput 中的参数(映射 RC×Pout" role="presentation">RC×Pout 到 Rdq" role="presentation">Rdq )。
从上面括号中的映射关系可以看出,Mc" role="presentation">Mc、Ms" role="presentation">Ms、Loutput" role="presentation">Loutput 的参数量分别为 dq×C×C" role="presentation">dq×C×C、dq×Pin×Pout" role="presentation">dq×Pin×Pout、dq×C×Pout" role="presentation">dq×C×Pout。当分组采样策略由 2 组、每组 64 点组成时,变量的值为 dq=256" role="presentation">dq=256、C=64" role="presentation">C=64、Pin=64" role="presentation">Pin=64 和 Pout=128" role="presentation">Pout=128,进而计算出 Mc" role="presentation">Mc、Ms" role="presentation">Ms 和 Loutput" role="presentation">Loutput 的参数数量均超过百万。
事实上,这三个组件共同占据了以ResNet-50
为主干的AdaMixer
模型总参数的大部分,同时它们也是增强模型性能的主要驱动力。综上,这也就是论文对它们进行LORS
实验的动机。
Analysis on Parameter Reduction
定义 W∈Rd×h" role="presentation">W∈Rd×h 为堆叠结构中每层都存在的权重参数,N" role="presentation">N 为堆叠层数:
- 如果是静态的,则原本就有 d×h" role="presentation">d×h 个参数,而使用 LORST" role="presentation">LORST 后平均每层仅需要 1N×d×h+K×(d×r+r×h)" role="presentation">1N×d×h+K×(d×r+r×h)个参数。
- 如果是自适应的,通过 q" role="presentation">q 线性变换生成需要 dq×d×h" role="presentation">dq×d×h 个参数,其中 dq" role="presentation">dq 是 q" role="presentation">q 的维度,使用 LORSA" role="presentation">LORSA 每层平均仅需要 1N×dq×d×h+K×(dq×r2+d×r+r×h)" role="presentation">1N×dq×d×h+K×(dq×r2+d×r+r×h) 个参数。
为了更直观地展示LORS
的参数减少效果,在ASM
设置 dq=256" role="presentation">dq=256,d=64" role="presentation">d=64,h=128" role="presentation">h=128,K=2" role="presentation">K=2,h=256" role="presentation">h=256,Loutput" role="presentation">Loutput 设置 d=2×128×128" role="presentation">d=2×128×128,K=1" role="presentation">K=1 的情况下,LORST" role="presentation">LORST 和 LORSA" role="presentation">LORSA 对不同 r" role="presentation">r 值的参数减少情况如表 1 所示。
Experiments
Implementation Details
使用权重衰减为 0.0001 的AdamW
优化器,在 8 个Nvidia V100 GPU
上训练所有模型,批量大小为 16,学习率为 2.5 × e−5。模型训练 12 或 36 个 周期,对于 12 周期训练,学习率在第 8 和 11 周期下降 10 倍;对于 36 周期训练,在第 24 和 33 周期学习率下降 10 倍。
低秩值方面,LORSA" role="presentation">LORSA 设置为 r=16" role="presentation">r=16,LORST" role="presentation">LORST 设置为 r=8" role="presentation">r=8。
所有实验中,LORSA" role="presentation">LORSA 的参数组数量设置为K=[1,1,2,2,3,3]" role="presentation">K=[1,1,2,2,3,3],应用于AdaMixer
解码器中的ACM
和ASM
,而Loutput" role="presentation">Loutput则设置为 K = [1,1,1,1,1,1]" role="presentation">K = [1,1,1,1,1,1]。
组采用时将特征通道分为 2 组,每组 64 个采样点,而不是AdaMixer
默认的 4 组,每组 32 个采样点,旨在增加LORS
的参数可压缩空间。根据AdaMixer
论文和论文的实验,这并不会简介提高性能。
主干网络使用ImageNet-1k
预训练模型进行初始化,LORS
参数初始化如下面所示,其余参数则由Xavier
初始化。关于模型训练的所有其他方面,如数据增强、损失函数等,只需遵循AdaMixer
的设置即可。
-
Initialization Strategies
论文对LORS
中的各个组件尝试了多种初始化方法,确定了整体的初始化方法如下:
- LORST" role="presentation">LORST:对于静态
LORS
,对 Wshared" role="presentation">Wshared 和每个 B" role="presentation">B 采用Kaiming
初始化,并对每个 A" role="presentation">A 进行零初始化。
- LORSA" role="presentation">LORSA:对于自适应
LORS
,将Kaiming
初始化每个 W~shared" role="presentation">~Wshared、B^" role="presentation">^B 和 A^" role="presentation">^A线性变换权重。此外,论文对每个 E^" role="presentation">^E 的线性变换权重使用零初始化
Main Results
表 2 展示了在 1× 训练方案下使用和不使用LORS
技术的性能比较。
表 3 展示了AdaMixer
+LORS
方法在不同骨干网和查询数的3×训练方案下的性能。
Ablation Study
表 4 展示了 LORSA" role="presentation">LORSA 和 LORST" role="presentation">LORST 对模型参数和性能的影响进行消融研究
表 5 对比了共享权重和私有权重中哪一个对解码器的性能影响更大。
表 6 探索了自适应LORS
的最佳参数组数量和秩 r" role="presentation">r 的值。
表 7 研究了 LORST" role="presentation">LORST 的最佳配置设置。
表 8 对结合LORS
结构的最佳解码器层数进行实验对比。
如果本文对你有帮助,麻烦点个赞或在看呗~
更多内容请关注 微信公众号【晓飞的算法工程笔记】