Cut Your Losses in Large-Vocabulary Language Models
该论文提出了一种名为Cut Cross-Entropy(CCE)的方法,通过避免在全局内存中存储所有token的logits来显著降低大型语言模型(LLM)训练时的内存占用。
更新于 2024-11-15。作者:Erik Wijmans、Brody Huval、Alexander Hertzberg、Vladlen Koltun、Philipp Krähenbühl。
中文摘要
随着语言模型规模的不断扩大,它们的词汇量也在增加。这使得LLM在训练过程中的内存占用不均衡,主要集中在单个层:损失计算中的交叉熵层。交叉熵会构建一个对数矩阵,其中包含每对输入标记和词汇项的条目,对于小型模型来说,它占用的内存量比LLM的其余部分多出一个数量级。我们提出了Cut Cross-Entropy(CCE)方法,该方法在计算交叉熵损失时,无需将所有标记的对数值实际加载到全局内存中。相反,CCE只计算正确标记的对数值,并实时评估所有对数值的log-sum-exp。我们实现了一个定制的内核,它使用闪存执行矩阵乘法和词汇中的log-sum-exp缩减,使得交叉熵计算的全球内存消耗可忽略不计。这产生了显著的效果。以Gemma 2(2B)模型为例,CCE将损失计算的内存占用从24 GB降低到1 MB,并将分类器头部的总训练时间内存消耗从28 GB降低到1 GB。为了提高CCE的吞吐量,我们利用softmax的固有稀疏性,并建议跳过对梯度计算有微小(即低于数值精度)贡献的元素。实验表明,在不牺牲训练速度或收敛性的情况下,实现了内存消耗的显著降低。
英文摘要
As language models grow ever larger, so do their vocabularies. This has shifted the memory footprint of LLMs during training disproportionately to one single layer: the cross-entropy in the loss computation. Cross-entropy builds up a logit matrix with entries for each pair of input tokens and vocabulary items and, for small models, consumes an order of magnitude more memory than the rest of the LLM combined. We propose Cut Cross-Entropy (CCE), a method that computes the cross-entropy loss without materializing the logits for all tokens into global memory. Rather, CCE only computes the logit for the correct token and evaluates the log-sum-exp over all logits on the fly. We implement a custom kernel that performs the matrix multiplications and the log-sum-exp reduction over the vocabulary in flash memory, making global memory consumption for the cross-entropy computation negligible. This has a dramatic effect. Taking the Gemma 2 (2B) model as an example, CCE reduces the memory footprint of the loss computation from 24 GB to 1 MB, and the total training-time memory consumption of the classifier head from 28 GB to 1 GB. To improve the throughput of CCE, we leverage the inherent sparsity of softmax and propose to skip elements of the gradient computation that have a negligible (i.e., below numerical precision) contribution to the gradient. Experiments demonstrate that the dramatic reduction in memory consumption is accomplished without sacrificing training speed or convergence.
解读
一句话总结
该论文提出了一种名为Cut Cross-Entropy(CCE)的方法,通过避免在全局内存中存储所有token的logits来显著降低大型语言模型(LLM)训练时的内存占用。
问题1:这篇论文想要解决什么具体问题?
• 问题背景:随着语言模型规模的扩大,其词汇量也在增加,导致训练时内存占用主要集中在交叉熵损失计算层,这一层的内存占用与词汇量呈指数增长。 • 现有方案不足:现有方法无法有效降低交叉熵损失计算层的内存占用,导致大规模语言模型训练时内存资源受限。 • 研究目标:提出一种方法,在保证训练速度和收敛性的前提下,显著降低大型语言模型训练时的内存占用。
问题2:论文的核心创新点是什么?
• 技术创新:提出CCE方法,通过在片上SRAM中计算logits,避免在全局内存中存储所有token的logits。 • 方法改进:对交叉熵损失计算进行优化,将交叉熵损失分解为索引矩阵乘法和log-sum-exp操作,并利用GPU的并行计算能力进行高效计算。 • 优势:与现有方法相比,CCE方法显著降低了内存占用,同时保持了训练速度和收敛性。
问题3:实验结果如何验证了方法的有效性?
• 关键实验:在Gemma 2(2B)模型上,CCE将损失计算的内存占用从24 GB降低到1 MB,将分类器头部的总训练时间内存消耗从28 GB降低到1 GB。 • 性能提升:实验结果表明,CCE方法在不牺牲训练速度和收敛性的前提下,显著降低了内存占用。 • 对比结果:与基线方法相比,CCE方法在内存占用方面具有显著优势。
问题4:这个研究的实际应用价值是什么?
• 应用场景:CCE方法适用于大型语言模型的训练,可以显著降低训练时的内存占用,从而支持更大规模的模型训练。 • 实施建议:在实际部署中,可以根据具体需求和硬件环境选择合适的CCE实现方式。 • 局限与展望:CCE方法主要针对交叉熵损失计算层进行优化,未来可以考虑将其扩展到其他类型的损失计算,以提高整体训练效率。