AutoChunk: Automated Activation Chunk for Memory-Efficient Deep Learning Inference

Xuanlei Zhao, Shenggan Cheng, Guangyang Lu, Haotian Zhou, Bin Jia, Yang YouICLR 2024

TL;DR

AutoChunk is an automated compiler system that slashes activation memory usage by over 80% during long-sequence inference with less than a 10% impact on speed. By automatically identifying and optimizing "chunking" strategies across a model's computation graph, it allows researchers to run sequences up to 11.7x longer on the same hardware.

The Problem: The Activation Memory Wall

As deep learning models scale, they inevitably hit a "memory wall." While researchers have developed many techniques to manage parameter memory (the weights of the model), activation memory—the storage required for intermediate tensors during a forward pass—remains a massive bottleneck.

For tasks involving long sequences, such as processing extensive documents in GPT or high-resolution images in Vision Transformers (ViT), activation memory grows exponentially. While "chunking" (breaking a large tensor into smaller pieces and processing them sequentially to save peak memory) is a known solution, implementing it manually is a labor-intensive, expert-only task. Manual chunking is often suboptimal, highly model-specific, and fails to account for the complex trade-offs between memory savings and computational speed.

Methods: Automating the Chunking Process

The authors introduce AutoChunk, a compiler-based system that automates the chunking process. It analyzes the model and applies chunking strategies automatically through three distinct passes:

AutoChunk compiler and runtime workflow
  1. Estimation Pass: The system analyzes the model's computation graph (using PyTorch FX) to estimate activation memory costs. It identifies "peak memory nodes"—the specific points where the model is most likely to run out of RAM.
  2. Chunk Search Pass: Using a bottom-up breadth-first search (BFS) algorithm, the system explores the "chunk space." It identifies "legal" chunk regions by following "Chunk Flows"—paths where dimensions can be split and computed sequentially without changing the final mathematical output.
  3. Chunk Selection Pass: Because there are many valid ways to chunk a model, AutoChunk uses Dynamic Programming (DP) to select the optimal strategy. It employs a custom cost function that balances "Macro" factors (number of nodes and FLOPs) and "Micro" factors (computation density and memory strides) to minimize speed loss while staying strictly under a user-defined memory budget.

Finally, the system uses code generation to automatically recompile the model into a memory-efficient version. For the end user, this requires only a single line of code: model = autochunk(model, memory_budget).

Evaluated Models

The authors evaluated AutoChunk on four diverse, state-of-the-art models representing different domains, all running on NVIDIA Tesla A100 80GB GPUs:

  • GPT (NLP): Prefill stage for long-sequence text.
  • ViT (Vision): Vision Transformer for image processing.
  • AlphaFold (Science): Protein structure prediction.
  • UNet (Generative AI): The variant used in Stable Diffusion.

Key Insights

  • The 70/30 Rule: The authors observed that over 70% of nodes in a model consume less than 30% of the peak memory. This means you don't need to chunk the entire model; targeting just the "heavy" 30% of nodes yields massive memory savings with minimal overhead.
  • Automating the "Expert": By defining formal rules for "Chunk Flow" (Basic Rule, Output Alignment, Flow Traceability, and Unique Setting), the authors successfully turned a manual engineering task into a searchable optimization problem.
  • Beyond Fused Kernels: While tools like FlashAttention reduce memory for specific modules, AutoChunk is more general. It finds memory savings in parts of the model that fused kernels don't touch, and it can even be used on top of fused kernels to save an additional 70% of memory.
  • Speed Sensitivity: Chunking isn't just about FLOPs; memory strides (how data is laid out in RAM) significantly impact I/O costs. AutoChunk's "Micro" cost function specifically accounts for this to prevent massive slowdowns.

Key Results

Activation memory vs. sequence length for various models
  • Memory Reduction: AutoChunk reduced activation memory by over 80% across the evaluated models.
  • Sequence Length Extension: It extended the maximum sequence length by 3.2x to 11.7x. For example, on a GPT model where the baseline crashed at a certain length, AutoChunk allowed it to process sequences nearly 12 times longer.
  • Efficiency: These massive memory gains came with a speed loss of less than 10%. In some large-sequence scenarios, AutoChunk actually accelerated inference by managing memory more efficiently than the standard baseline.
  • Outperforming Experts: When tested against the manually designed chunking strategy in OpenFold (an AlphaFold implementation), AutoChunk was both faster (9.2%–14.6% speedup) and more memory-efficient (reducing memory by an additional 30%+).

Limitations and Future Work

  • Inference Focus: The current implementation is specifically designed for inference. While the logic could be adapted for training, training involves backpropagation, which keeps activations in memory much longer and presents a more complex optimization challenge.
  • Compiler Overhead: While the runtime is fast, the initial compilation passes take time. The authors mitigated this by using a "local window" search to reduce complexity from $O(N^3)$ to $O(N)$.
  • Future Directions: The authors plan to adapt AutoChunk for training scenarios, potentially combining it with activation checkpointing to further break the memory wall for massive model training.

Summary updated

BibTeX
@inproceedings{zhao2024autochunk,
  author = {Xuanlei Zhao and Shenggan Cheng and Guangyang LU and Haotian Zhou and Bin Jia and Yang You},
  title = {{AutoChunk: Automated Activation Chunk for Memory-Efficient Deep Learning Inference}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=GQGNLEHmdl},
}
Figure