Cut Your Losses in Large-Vocabulary Language Models

Erik Wijmans, Brody Huval, Alexander Hertzberg, Vladlen Koltun, Philipp Krähenbühl

TL;DR

Cut Your Losses introduces Cut Cross-Entropy (CCE), a memory-efficient implementation of the cross-entropy loss that eliminates the need to store massive logit matrices in GPU memory. By computing logits on-the-fly in on-chip SRAM, CCE reduces the loss layer's memory footprint by orders of magnitude—shrinking it from gigabytes to megabytes—and enables up to a 10x increase in training batch sizes without sacrificing speed or convergence.

What Problem Does This Paper Address?

In the race to build better Large Language Models (LLMs), researchers are scaling three things: parameter count, context length, and vocabulary size. While techniques like FlashAttention have tamed the memory costs of long contexts, the cross-entropy loss layer has become a new, massive bottleneck.

To compute this loss, the model generates a "logit matrix" of size $N \times |V|$, where $N$ is the number of tokens in a batch and $|V|$ is the vocabulary size. For a model like Gemma 2 (2B) with a 256k-token vocabulary, this single matrix can consume 24 GB of GPU memory. This "memory hog" accounts for up to 90% of the total training memory in smaller models, severely limiting the batch sizes researchers can use and forcing them to rely on expensive hardware or complex memory-saving tricks that often slow down training.

Methods

The authors propose Cut Cross-Entropy (CCE), which draws inspiration from the "tiling" approach of FlashAttention. Instead of materializing the entire logit matrix in the GPU's global memory (DRAM), CCE performs the computation in custom CUDA/Triton kernels that keep data in fast, on-chip SRAM.

Key components include:

  • On-the-fly Log-Sum-Exp (LSE): The loss only requires the logit of the ground-truth token and the LSE of all logits. CCE computes blocks of logits, performs the LSE reduction locally in SRAM, and only writes the final scalar result back to DRAM.
  • Gradient Filtering: The authors observe that the softmax distribution is extremely sparse—in frontier models, less than 0.02% of elements are non-zero. CCE skips the gradient computation for blocks where the softmax values fall below numerical precision ($\epsilon = 2^{-12}$), resulting in a 3.5x speedup in the backward pass.
  • Vocabulary Sorting: To maximize the benefits of filtering, they heuristically sort the vocabulary by average logit value. This groups "active" tokens together into dense blocks, allowing the kernel to skip entire empty blocks of the vocabulary more effectively.
  • Numerical Stability: For pretraining, where precision is critical, they incorporate Kahan summation to prevent rounding errors during the global reduction of gradients.
Figure 2: Access patterns and computation of blockwise (a) indexed matrix multiplication, (b) linear-log-sum-exp forward pass, and (c) linear-log-sum-exp backward pass. See Algorithms 1 to 3 for the corresponding algorithms.

Datasets

The researchers evaluated CCE across a wide range of frontier models (Gemma 2, Llama 2/3, Phi 3.5, Mistral, Qwen) using:

  • Alpaca Dataset: Used for fine-tuning experiments to demonstrate that CCE matches the convergence of standard implementations.
  • Open WebText: A 5% subset was used for pretraining stability tests to ensure that the gradient filtering and numerical approximations don't hurt long-term model performance.

Key Insights and Ideas

  • DRAM is the Bottleneck, Not Compute: It is actually faster to recompute logits in SRAM than it is to write them to and read them back from DRAM. CCE exploits this by trading a bit of extra arithmetic for a massive reduction in memory bandwidth requirements.
  • The "FlashAttention" for Loss: Just as FlashAttention avoided the $N \times N$ attention matrix, CCE avoids the $N \times |V|$ logit matrix. This suggests a broader trend in systems research: moving away from materializing large intermediate tensors.
  • Softmax Sparsity is a Superpower: The fact that the vast majority of vocabulary tokens have near-zero probability is a structural property that can be exploited for significant algorithmic speedups without losing accuracy.

Key Results

  • Massive Memory Savings: For the Gemma 2 (2B) model, CCE reduced the loss computation memory from 24 GB to just 1 MB. The total memory for the classifier head dropped from 28 GB to 1 GB.
  • Increased Batch Size: By freeing up memory, CCE allows for much larger batches. For GPT-2 and Gemma 2 (2B), the maximum batch size increased by ~10x (see Figure 1).
  • No Speed Penalty: CCE is roughly 5% faster than torch.compile in the forward pass and maintains competitive speeds in the backward pass, even with the extra recomputation.
  • Identical Convergence: Training curves for CCE and standard PyTorch implementations are virtually indistinguishable, proving that gradient filtering is safe for both fine-tuning and pretraining.
Figure 1: Memory use and maximum attainable batch size (in millions of tokens) for a vari- ety of frontier models on a 16-GPU (80 GB each) fully-sharded data-parallel setup (Rajbhan- dari et al., 2020) with activation checkpointing (Chen et al., 2016) and a mixed-precision 16-bit (fp16/bf16) AdamW optimizer (Kingma & Ba, 2015; Loshchilov & Hutter, 2019). For each model, we break its memory use down into weights and optimizer states, activation checkpoints, and the log-probabilities computed by the cross-entropy loss layer. Our Cut Cross-Entropy (CCE) enables increasing the batch size by 1.5x (Llama 2 13B) to 10x (GPT 2, Gemma 2 2B), with no sacrifice in speed or convergence. Exact values in Table A4.

Limitations and Future Work

  • Triton Constraints: The current implementation uses Triton, which limits control flow to the block level. The authors suggest that a pure CUDA implementation could allow for even finer-grained gradient filtering and further speed gains.
  • Precision in Pretraining: While fine-tuning is robust, pretraining requires the more expensive Kahan summation to maintain stability. Finding a more efficient way to handle this precision could further optimize the backward pass.
  • Broader Applications: The authors propose extending CCE to other large-scale classification problems, such as high-resolution image classification or contrastive learning (e.g., CLIP), where the number of "classes" is similarly massive.

Summary updated

BibTeX
@inproceedings{wijmans2025cut,
  author = {Erik Wijmans and Brody Huval and Alexander Hertzberg and Vladlen Koltun and Philipp Krähenbühl},
  title = {{Cut Your Losses in Large-Vocabulary Language Models}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=E4Fk3YuG56},
}
Figure