arXiv:2411.09009cs.LGcs.CL2024-11ICLR被引 36

提出CCE方法,大幅降低大模型训练时的内存占用。

Cut Your Losses in Large-Vocabulary Language Models

  • 只计算正确词的logit,动态计算log-sum-exp,避免存储全部logits。
  • Gemma 2(2B)模型训练内存从24GB降至1MB,总分类器内存从28GB降至1GB。
  • 利用softmax稀疏性加速梯度计算,适合大词汇量模型高效训练。

随着语言模型规模增大,其词表也不断扩张,导致训练时的内存开销主要集中在交叉熵损失计算上。传统方法需构建包含所有输入词与词表项组合的logit矩阵,对小型模型而言,其内存消耗比整个模型还高出一个数量级。本文提出剪裁交叉熵(Cut Cross-Entropy, CCE),无需将所有词的logit存入全局内存,而是仅计算正确词的logit,实时进行log-sum-exp归约。通过自定义内核在闪存中完成矩阵乘法与词汇维度的log-sum-exp操作,使交叉熵计算的全局内存占用几乎可忽略。以Gemma 2(2B)为例,该方法将损失计算内存从24 GB降至1 MB,分类头总训练内存从28 GB降至1 GB。为进一步提升吞吐率,我们利用softmax的固有稀疏性,跳过对梯度贡献低于数值精度的元素。实验表明,该方法在不牺牲训练速度和收敛性的前提下实现了内存的大幅压缩。

原文摘要 · Abstract (English)

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.

大模型训练内存优化交叉熵

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。