概述
它将 LoRA 的低成本与专家混合模型的专业化相结合,因此一个基本模型可以在 1% 的参数下训练时处理多项任务。
主要要点
- LoRA 专家将多个低级适配器连接到冻结模型,并使用路由器对每个输入进行加权。
- 根据 2021 年的论文,与完全微调的 GPT-3 175B 相比,LoRA 将可训练参数减少了 10,000 倍,GPU 内存减少了三倍。
- Cohere 2023 年的研究匹配了完全微调,同时使用轻量级专家对 110 亿参数模型进行了不到 1% 的更新。
- 动态路由会阻止将适配器合并到基本权重中,因此 LoRA 专家的混合会增加一些推理成本,而单个合并的 LoRA 可以避免这种成本。
- 负载平衡损失使路由器无法将几乎所有令牌发送给少数专家,这种故障称为专家崩溃。
深入探讨
Mixture of LoRA experts combines two established ideas. The first is LoRA, low-rank adaptation, introduced by Edward Hu and colleagues in June 2021. Instead of retraining all the weights of a large model, LoRA freezes them and learns a small correction for selected weight matrices, written as the product of two thin matrices. For GPT-3 175B, the LoRA paper reports 10,000 times fewer trainable parameters and three times less GPU memory than full fine-tuning, with no added inference delay once the correction is merged into the weights. The second idea is the mixture of experts (MoE): a model contains several sub-networks, called experts, and a small gating network, or router, chooses which ones to use for each input. Noam Shazeer and colleagues showed in 2017 that sparsely gated MoE layers with up to thousands of experts could raise model capacity more than 1,000-fold with little extra computation.
A mixture of LoRA experts uses LoRA adapters as the experts. Step by step: a pretrained model is frozen; at chosen layers, usually the attention projections or the feed-forward block, several LoRA adapters are attached side by side; a router, typically one small linear layer followed by a softmax, reads the current token's hidden state and outputs a weight for each adapter; each adapter computes its low-rank correction; and the corrections are added to the frozen layer's output in proportion to the router's weights. With sparse routing, only the top one or two adapters are computed for each token. During training only the adapters and router are updated.
The reason to do this rather than train one bigger adapter is interference. When one adapter is trained on a mixture of very different tasks, such as coding, translation and arithmetic, the tasks compete for the same few parameters. Separate experts let different kinds of input use different parameters. LoRAMoE, posted in December 2023 by Shihan Dou and colleagues, targeted a specific version of this problem: scaling up instruction data can damage the world knowledge a model learned in pretraining. It splits its LoRA experts so some focus on preserving existing knowledge and others on new tasks, and reports gains on downstream tasks while retaining stored knowledge.
The approach developed through several papers. In September 2023 Ted Zadouri and colleagues at Cohere, in "Pushing Mixture of Experts to the Limit", built mixtures of lightweight experts including LoRA-based ones and updated less than 1% of an 11-billion-parameter model, outperforming standard parameter-efficient methods and matching full fine-tuning on instruction tuning. LoraHub, posted in July 2023 by Chengsong Huang and colleagues, took a different route: it combines LoRA modules already trained on separate tasks, choosing fixed mixing weights from a few examples of a new task with a gradient-free search. MixLoRA, from April 2024, added top-k routed LoRA experts inside feed-forward blocks plus separate LoRA adapters on attention, with a load-balancing loss so the router does not overuse a few experts. In April 2024 Xun Wu, Shaohan Huang and Furu Wei described Mixture of LoRA Experts (MoLE), which learns gating over existing trained LoRAs and was tested in both language and vision-and-language settings.
Compared with the alternatives, the trade-offs are clear. Full fine-tuning updates every weight and adapts best, but needs far more memory and produces a full copy of the model per task. A single LoRA adapter is cheapest to train and serve but can struggle with many mixed tasks. Merging several LoRAs by simple weighted addition, a common shortcut, is free at inference but, according to the MoLE paper, can weaken the base model's generation and blur each LoRA's distinct behavior. Keeping separate adapters and switching between them by hand works when the task is known in advance, but requires an outside decision about which adapter to use. A learned router makes that choice automatically, per token, at the cost of extra machinery.
The approach has real limitations. Routers can collapse, sending almost all tokens to one or two experts and leaving the others untrained, which is why methods such as MixLoRA add load-balancing losses. Input-dependent routing removes one of LoRA's main advantages: because the mixture changes token by token, the adapters cannot be merged into the base weights ahead of time, so inference does extra work. Batching requests that use different adapters complicates serving, which is the problem S-LoRA addresses. Reported gains are mostly self-reported on academic benchmarks with specific base models, and results vary with the number of experts, rank, routing method and which layers receive experts.
In practice the costs are modest. Each LoRA expert adds two thin matrices per adapted weight matrix, so even several experts usually total well under the 1% of model parameters reported in the Cohere work. Training fits on the same hardware used for ordinary LoRA fine-tuning, plus a little memory for the extra adapters and router. The main engineering decisions are how many experts to use, their rank, whether routing is dense or top-k, and which layers to adapt. Serving many adapters efficiently benefits from systems built for it; S-LoRA reports serving thousands of concurrent adapters with up to four times the throughput of naive approaches in Hugging Face PEFT and vLLM.
04工作示例
计算小型 LoRA 混合物中的参数
以大小为 4,096 x 4,096 的注意力投影为例,其中包含约 1680 万个冻结权重。
添加一个 8 级 LoRA 适配器:A 为 8 x 4,096,B 为 4,096 x 8,提供 65,536 个可训练参数。
使其混合 4 个这样的专家:4 乘以 65,536 就是 262,144 个参数。
添加一个路由器,将 4,096 维输入映射到 4 个门得分:4,096 乘以 4 等于 16,384 个参数。
总可训练参数:278,528,约占冻结矩阵的 1.7%。
使用 top-2 路由,为每个标记计算两个适配器校正而不是四个,并将额外的工作与基本矩阵乘法进行比较。
它显示了什么
一份具体预算显示,四位专家加上一台路由器的成本低于一个权重矩阵的 2%,并且 top-2 路由将适配器计算量减半。通过在 PyTorch 中构建层、冻结 W0 并对设置了 require_grad 的参数求和 numel() 来检查它。
现实世界的实施
Cohere 研究人员在 2023 年表明,接受过指令训练的 LoRA 专家组合更新了 110 亿参数模型的不到 1%,但其性能与完全微调相当。
LoRAMoE(2023 年 12 月)让一些 LoRA 专家负责保存模型存储的世界知识,而其他专家则学习新任务,从而减少大型指令数据集造成的遗忘。
LoraHub (COLM 2024) 结合了经过不同任务训练的 LoRA 模块,可以解决一些示例中未见过的 Big-Bench Hard 任务,无需梯度或额外参数。
MixLoRA 将 top-k 路由的 LoRA 专家添加到冻结模型的前馈层中,并报告多任务精度比标准参数高效方法高出约 9%。
S-LoRA 是为许多适配器构建的服务系统,可为数千个并发 LoRA 适配器提供服务,其吞吐量高达 Hugging Face PEFT 和 vLLM 中原始服务的四倍。
LoRA 专家混合的未来
Research since 2023 has explored several directions at once: jointly trained LoRA mixtures for instruction tuning (Cohere, September 2023), mixtures that protect pretrained knowledge (LoRAMoE, December 2023), composition of independently trained adapters (LoraHub, published at COLM 2024, and MoLE, April 2024) and sparse top-k LoRA experts with load balancing (MixLoRA, 2024). Serving infrastructure has advanced in parallel, with S-LoRA showing that thousands of adapters can share one base model on the same hardware. Open problems include stable routing without expert collapse, principled choices for the number and rank of experts, fair comparison across papers that use different base models and benchmarks, and keeping latency low when adapters cannot be merged into the base weights. LoraHub's authors proposed a platform where users share trained LoRA modules for reuse on new tasks; whether shared adapter libraries with automatic routing become common practice, or whether single well-trained adapters and larger base models remain simpler and good enough, is still an open question that depends on evidence from production use rather than benchmarks alone.
付诸实践
为什么这很重要
- 成本与预算. 多年来,架构决策决定着性能和运营成本。
- 更清晰的判决. 技术教育帮助团队选择正确的堆栈,而不仅仅是最新的堆栈。
- 质量控制. 更好的工程选择可以减少生产中的可靠性事故。
留意
- 优化一项基准测试可以隐藏更广泛的系统弱点。
- 基础设施和维护成本常常被低估。
- 随着系统变得更加复杂,安全性和可观察性差距可能会扩大。
后续步骤
- 在实施之前定义延迟、质量和成本目标。
- 在实际负载和数据条件下进行基准测试。
- 仪器监控错误、漂移和用户影响。
- 在扩展之前准备回滚和事件响应路径。
资料来源与延伸阅读
- arXivLoRA:大型语言模型的低秩适应
- arXiv极其庞大的神经网络:稀疏门控专家混合层
- arXiv (Cohere)将专家混合推向极限:用于指令调优的极其参数高效的 MoE
- arXivLoRAMoE:通过 MoE 风格的插件减轻大型语言模型中的世界知识遗忘
- arXiv (COLM 2024)LoraHub:通过动态 LoRA 组合实现高效的跨任务泛化
- arXivLoRA 专家的混合体
- arXivMixLoRA:利用基于 LoRA 的专家组合增强大型语言模型微调
- arXivS-LoRA:为数千个并发 LoRA 适配器提供服务
不断探索
常见问题
什么是 LoRA 专家组合?
LoRA 专家混合是一系列微调方法,将多个小型低级适配器(称为 LoRA 专家)附加到冻结语言模型,并使用学习路由器来决定每个适配器对每个输入贡献多少。它将 LoRA 的低成本与专家混合模型的专业化相结合,因此一个基本模型可以在 1% 的参数下训练时处理多项任务。
Mixture of LoRA Experts 中的“LoRA”部分指的是什么?
LoRA 代表低秩适应:它冻结基本模型并训练紧凑的低秩矩阵,以低成本调整行为。
MoLE 中的门控/路由器网络的作用是什么?
路由器对可用的 LoRA 专家产生权重,根据输入(通常是每层或令牌)选择并混合它们。
为什么 MoLE 通常比在许多混合任务上训练一个适配器更好?
当一个适应者必须同时学习许多相互冲突的技能时,单独的专家可以避免灾难性的遗忘和任务干扰。
在 MoLE 训练期间,基本模型的预训练权重通常会发生什么情况?
脊椎骨仍然冻结;只有小型 LoRA 专家和门控网络接收梯度更新。
在扩散图像模型中,MoLE 中的分层/每层门控有助于解决什么问题?
每层门控了解每个适配器在每一层的贡献程度,让多个概念 LoRA 结合在一起,而不会由一个适配器占主导地位。