Mini-Sequence Transformers: Optimizing Intermediate Memory for Long Sequences Training

Cheng Luo, Jiawei Zhao, Zhuoming Chen, Beidi Chen, Anima Anandkumar

Training Large Language Models (LLMs) on extremely long sequences is a notorious memory hog. While innovations like FlashAttention have successfully tamed the quadratic memory growth of the attention mechanism, a "hidden" bottleneck remains: the intermediate activations within the Multilayer Perceptron (MLP) and Language Modeling Head (LM-Head) layers.

In the paper "Mini-Sequence Transformers: Optimizing Intermediate Memory for Long Sequences Training," researchers introduce a simple yet highly effective solution called the Mini-Sequence Transformer (MsT). By partitioning input sequences into smaller chunks, MsT allows for training with context lengths 12x to 24x longer than standard implementations on a single GPU, without sacrificing throughput or model accuracy.

The Hidden Memory Bottleneck

The push for longer context windows—necessary for processing entire books, long codebases, or complex legal documents—is fundamentally limited by GPU memory. Even with optimized attention mechanisms, the intermediate values generated in linear layers can be massive.

For instance, in the Llama3 architecture, the MLP intermediate state is roughly 7 times larger than the input itself, and the LM-Head intermediate state can be a staggering 32 times larger. Standard training paradigms store all of these activations for the backward pass, leading to "Out of Memory" (OOM) errors long before the GPU's actual compute capacity is fully utilized. Existing solutions like gradient accumulation work across the batch dimension, but they fail to solve the memory crisis for a single extremely long sequence.

The Mini-Sequence Methodology

The core innovation of MsT is sequence partitioning. Because MLP and LM-Head layers operate on individual tokens independently (unlike the attention mechanism, which must contextualize the entire sequence at once), a long sequence can be safely divided into smaller "mini-sequences."

Comparison between conventional Transformer architecture and the proposed MsT approach.

The MsT process works as follows:

  1. Partitioning: The full input sequence is divided into $M$ smaller chunks.
  2. Iterative Processing: Each mini-sequence is processed through the MLP or LM-Head sequentially.
  3. Reconstruction: The outputs are concatenated back together to form the complete sequence output.
  4. Activation Recomputation: MsT is designed to synergize perfectly with gradient checkpointing. During the backward pass, the model only needs to recompute and store activations for one mini-sequence at a time. This drastically lowers the peak memory footprint.

To prevent performance degradation on shorter sequences, the authors implement a Chunk-based MsT, which only triggers the partitioning logic when the sequence length exceeds a specific threshold (typically the model's hidden dimension).

Key Results and Performance

The researchers evaluated MsT across several popular LLM architectures, including Llama3-8B, Llama2-7B, Mistral-7B, Qwen2-7B, and Gemma-2-9B, using a single NVIDIA A100-80GB GPU. The results demonstrate a massive leap in memory efficiency.

Maximum sequence size achieved by different models using MsT vs. baselines.
  • Massive Context Expansion: MsT consistently outperformed both vanilla PyTorch and standard activation recomputation. For Llama3-8B, the maximum sequence length increased from 5k (vanilla) to 60k (MsT)—a 12x improvement. For Gemma-2-9B, MsT achieved a 24x increase in context length (from 1.5k to 36k).
  • Zero Throughput Degradation: Despite the iterative processing, there was no measurable drop in training speed. In some scenarios, MsT even slightly improved overall throughput by enabling larger effective batch sizes.
  • Lossless Quality: Because MsT does not alter the underlying mathematics of the Transformer—it merely changes the order of operations—it results in zero degradation in convergence. Training Llama3-8B on 30k context lengths using MsT yielded a 2.7x improvement in perplexity compared to an 8k baseline on long-context tasks.

Implementation and Future Directions

One of the most appealing aspects of MsT is that it is implementation-agnostic. It acts as a "wrapper-style" optimization, meaning it does not require rewriting complex CUDA kernels and can be integrated into existing frameworks like Hugging Face with minimal code changes. Furthermore, MsT is orthogonal to sequence parallelism, meaning it can be combined with distributed training tools like DeepSpeed-Ulysses to scale context lengths linearly across multiple GPUs.

While highly effective, the authors note that writing custom CUDA kernels for the partitioning logic could unlock even greater low-level memory savings in the future. Additionally, combining MsT with lossy techniques like 4-bit or 8-bit quantization could push context boundaries even further—initial tests showed Llama3 reaching up to 140k tokens when combining MsT with 4-bit quantization.

For researchers and engineers looking to push the boundaries of long-context LLMs without relying on massive server farms, the Mini-Sequence Transformer offers a practical, drop-in solution to the memory wall.

Summary updated

BibTeX
@inproceedings{luo2024mini,
  author = {Cheng Luo and Jiawei Zhao and Zhuoming Chen and Beidi Chen and Anima Anandkumar},
  title = {{Mini-Sequence Transformers: Optimizing Intermediate Memory for Long Sequences Training}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=2KuZHYykkq},
}
Figure