Cut Your Losses in Large-Vocabulary Language Models

Erik Wijmans, Brody Huval, Alexander Hertzberg, Vladlen Koltun, Philipp Kraehenbuehl

TL;DR

As LLM vocabularies grow, the cross-entropy loss layer has become a massive memory bottleneck during training, often consuming more memory than the rest of the model combined. This paper introduces Cut Cross-Entropy (CCE), a custom GPU kernel that computes the loss and gradients without materializing the full logit matrix in global memory. CCE reduces the memory footprint of the loss computation to near zero, enabling significantly larger batch sizes without sacrificing training speed or model convergence.

The Memory Bottleneck of Modern LLMs

Over the past few years, the AI community has developed brilliant techniques to reduce the memory footprint of transformer backbones: FlashAttention, activation checkpointing, and ZeRO sharding. However, these optimizations have inadvertently shifted the memory bottleneck to the very end of the model: the final classification head and the cross-entropy loss.

Memory breakdown of various LLMs showing log probabilities dominating memory

To predict the next token, an LLM computes a score (logit) for every single word in its vocabulary. This requires building a massive matrix of size [Batch Size × Sequence Length × Vocabulary Size]. With modern models expanding their vocabularies to 128K (Llama 3) or even 256K tokens (Gemma 2) to improve compression and multilingual performance, this single layer can now consume up to 90% of a model's memory footprint during training. For a small model like Gemma 2 (2B), just storing the log-probabilities for a standard batch can take 24 GB of GPU memory, severely limiting the batch sizes researchers can use.

Cut Cross-Entropy (CCE) Methodology

Standard cross-entropy implementations compute all logits, write them to the GPU's global memory (DRAM), apply the softmax function, and then compute the loss. The authors propose Cut Cross-Entropy (CCE) to bypass this DRAM bottleneck entirely.

Overview of the Cut Cross-Entropy forward and backward passes

Key techniques include:

  • On-the-fly SRAM Computation: Instead of saving the massive logit matrix to global memory, CCE computes blocks of logits directly in the GPU's ultra-fast, on-chip shared memory (SRAM). It calculates the log-sum-exp for the denominator of the softmax, updates a global state, and immediately discards the logits.
  • Decomposed Loss: The cross-entropy formula is mathematically rearranged so the model only explicitly computes the logit for the ground-truth token (using a highly efficient indexed matrix multiplication), while the rest of the vocabulary is only used for the log-sum-exp reduction.
  • Gradient Filtering: During the backward pass, CCE exploits the fact that softmax probabilities drop off exponentially. Any probability below the precision limit of bfloat16 (roughly $2^{-12}$) mathematically contributes nothing to the gradient. CCE simply skips the gradient computation for these blocks.
  • Vocabulary Sorting: To make the gradient filtering more efficient, CCE heuristically sorts the vocabulary by average logit. This groups the few "active" tokens together, allowing the GPU to skip massive, contiguous empty blocks of computation.

Key Insights

  • Softmax is overwhelmingly sparse: The most surprising insight is just how fast token probabilities vanish. By the ~50th most likely token, the probability is usually so close to zero that it falls below the numerical precision of bfloat16. This means >99.9% of the gradient computation in a standard cross-entropy layer is effectively multiplying by zero.
  • Compute is cheaper than memory bandwidth: Recomputing the logits in SRAM during the backward pass sounds like it would be slower than just reading them from memory. However, because GPU global memory (DRAM) is so much slower than on-chip compute, avoiding the DRAM read/write actually makes CCE highly competitive in terms of wall-clock time.
  • The "Final Boss" of LLM memory: Because we've optimized the attention mechanism so heavily, the simple linear classifier at the end of the network is now the primary barrier to scaling context lengths and batch sizes.

Results and Impact

  • Massive Memory Reduction: For the Gemma 2 (2B) model, CCE reduces the memory footprint of the loss computation from 24 GB down to just 1 MB. The total training-time memory of the classifier head drops from 28 GB to 1 GB.
  • Batch Size Scaling: By freeing up so much memory, CCE allows for dramatically larger batch sizes. On a 16-GPU setup, maximum batch sizes increased by 1.5x (Llama 2 13B) up to 14.9x (Gemma 2B).
  • Speed and Convergence: CCE is highly performant. It computes the forward loss slightly faster than torch.compile (46ms vs 49ms) and the combined forward+backward pass slightly slower (145ms vs 143ms). Across multiple models (Gemma, Llama, Mistral, Phi, Qwen), CCE produces loss curves and validation perplexities that are indistinguishable from standard PyTorch implementations.

Limitations and Future Work

  • Pretraining Precision: For pretraining from scratch, the standard CCE drops too much precision during global memory reductions. The authors had to implement a variant with Kahan summation (CCE-Kahan-FullC) to maintain stability. This variant uses slightly more memory for temporary buffers and is a bit slower, though this is easily offset by the larger batch sizes it enables.
  • Triton Limitations: CCE is implemented in Triton, which restricts control flow (like skipping blocks) to the block level. The authors note that a pure CUDA implementation could allow for finer-grained control and potentially even better performance.
  • Future Directions: The authors suggest this technique could be highly valuable for pipeline parallelism (where the massive memory footprint of the final layer currently makes balancing pipeline stages difficult) and could be adapted for other large-class problems like contrastive learning or image classification.

Summary updated

BibTeX
@inproceedings{wijmans2025cut,
  author = {Erik Wijmans and Brody Huval and Alexander Hertzberg and Vladlen Koltun and Philipp Kraehenbuehl},
  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