AutoChunk: Automated Activation Chunk for Memory-Efficient Long Sequence Inference

Xuanlei Zhao, Shenggan Cheng, Guangyang Lu, Jiarui Fang, Haotian Zhou, Bin Jia, Ziming Liu, Yang YouarXiv 2024 · DOI

Introduction

As deep learning models tackle increasingly complex tasks—like analyzing entire books, high-resolution images, or long protein chains—they hit a physical limit known as the "memory wall." While researchers have spent years optimizing parameter memory (the size of model weights), activation memory (the intermediate tensors stored during a forward pass) grows rapidly—sometimes exponentially—with the length of the input sequence.

Existing solutions have major drawbacks: quantization can introduce errors, fused kernels (like FlashAttention) are hard to generalize, and manual chunking is labor-intensive and often suboptimal due to complex data dependencies in modern computational graphs.

Methodology: The AutoChunk Pipeline

AutoChunk is an automated compiler system that tackles this problem by intelligently breaking down intermediate computations into smaller "chunks." Instead of a human deciding where to split a calculation, AutoChunk follows a three-stage pipeline:

  1. Estimation Pass: It analyzes the model's computational graph to identify "peak memory nodes"—the specific points where memory usage spikes.
  2. Chunk Search: Using a bottom-up breadth-first search (BFS), the system explores the graph to find "legal" chunk regions. It follows strict rules to ensure that splitting the computation won't change the final mathematical result.
  3. Chunk Selection: This stage uses Dynamic Programming (DP) to select the best chunking strategy. It balances "Macro" factors (number of nodes and FLOPs) with "Micro" factors (computation density and memory strides) to minimize speed loss.
The AutoChunk pipeline: from computational graph analysis to runtime chunk execution.

Finally, AutoChunk uses code generation (via PyTorch FX) to automatically recompile the model into a memory-efficient version.

Key Insights and Ideas

  • The 70/30 Rule: The authors observed that activation memory is distributed unevenly. More than 70% of nodes in a model consume less than 30% of the maximum memory. By optimizing just the "peak" 30% of nodes, AutoChunk can achieve a 70% reduction in total activation cost.
  • Beyond FLOPs: Traditional optimization often focuses on reducing floating-point operations. AutoChunk realizes that for chunking, memory stride (how data is laid out in memory) is often more important for speed than the raw number of operations.
  • Synergy with Fused Kernels: AutoChunk isn't a replacement for tools like FlashAttention; it's a partner. Even when using fused kernels, AutoChunk can reduce the remaining activation memory by another 70% by optimizing parts of the graph that manual kernels don't touch.

Key Results

The researchers evaluated AutoChunk across diverse architectures including GPT, ViT, AlphaFold, and UNet on NVIDIA Tesla A100 GPUs.

Activation memory reduction and sequence length extension across different models.
  • Massive Memory Savings: AutoChunk reduced activation memory by over 80% across various models.
  • Minimal Speed Penalty: When reducing memory by 50%, the throughput loss was often under 3%. Even at maximum compression (80% reduction), the speed loss remained under 10%.
  • Breaking the Wall: The system extended the maximum possible sequence length significantly: 11.7x extension for 1D inputs (GPT) and 3.2x extension on average for 2D inputs (AlphaFold, ViT, UNet).
  • Better than Humans: In head-to-head tests against expert-designed chunking strategies in OpenFold, AutoChunk was both faster (9.2% to 14.6% speedup) and more memory-efficient.

Conclusion

AutoChunk provides a highly efficient, automated solution for the activation memory bottleneck in long-sequence inference. By reducing activation memory by over 80% with minimal impact on speed, it allows models to process significantly longer sequences on existing hardware. While currently focused on inference, future work may adapt this logic for training by combining it with activation checkpointing.

Summary updated

Abstract

Large deep learning models have achieved impressive performance across a range of applications. However, their large memory requirements, including parameter memory and activation memory, have become a significant challenge for their practical serving. While existing methods mainly address parameter memory, the importance of activation memory has been overlooked. Especially for long input sequences, activation memory is expected to experience a significant exponential growth as the length of sequences increases. In this approach, we propose AutoChunk, an automatic and adaptive compiler system that efficiently reduces activation memory for long sequence inference by chunk strategies. The proposed system generates chunk plans by optimizing through multiple stages. In each stage, the chunk search pass explores all possible chunk candidates and the chunk selection pass identifies the optimal one. At runtime, AutoChunk employs code generation to automatically apply chunk strategies. The experiments demonstrate that AutoChunk can reduce over 80\% of activation memory while maintaining speed loss within 10%, extend max sequence length by 3.2x to 11.7x, and outperform state-of-the-art methods by a large margin.
BibTeX
@misc{zhao2024autochunk,
  author = {Xuanlei Zhao and Shenggan Cheng and Guangyang Lu and Jiarui Fang and Haotian Zhou and Bin Jia and Ziming Liu and Yang You},
  title = {{AutoChunk: Automated Activation Chunk for Memory-Efficient Long Sequence Inference}},
  year = {2024},
  eprint = {2401.10652},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2401.10652},
}
Figure