MODeL: Memory Optimizations for Deep Learning

Benoit Steiner, Mostafa Elhoushi, Jacob Kahn, James HegartyICML 2023

As deep neural networks (DNNs) grow exponentially in size—from early vision models to massive language models like GPT-3—the hardware used to train them is struggling to keep pace. While model parameters have exploded, GPU memory capacity has only grown linearly, creating a significant "memory wall" in AI development.

To cope with this limitation, researchers typically rely on trade-offs that either slow down training or degrade model quality. MODeL (Memory Optimizations for Deep Learning) introduces a novel optimization algorithm that automatically reduces the memory footprint of DNN training by an average of 30%. Crucially, it achieves this without sacrificing accuracy, requiring manual network modifications, or significantly increasing training time.

The Problem: Fragmentation and Inefficient Scheduling

When training large models, standard frameworks like PyTorch and TensorFlow often manage memory inefficiently. They typically allocate memory on a "first-come, first-served" basis. Over time, this greedy approach leads to memory fragmentation—a state where the memory is riddled with small, unusable "holes."

Memory fragmentation can cause out-of-memory errors even when total free space is sufficient.

Because of fragmentation, a new tensor might fail to fit into memory even if the total free space across the GPU is technically large enough. Furthermore, these frameworks execute operations in a fixed, predefined order that is rarely optimized for memory conservation.

Common workarounds include:

  • Spilling/Paging: Moving data back and forth to slower system RAM, which severely bottlenecks training speed.
  • Rematerialization (Gradient Checkpointing): Deleting intermediate activations and recomputing them during the backward pass, which increases overall computation time.
  • Reduced Precision/Pruning: Compressing the model, which risks hurting the final accuracy.

MODeL argues that before resorting to these trade-offs, we should first eliminate the waste inherent in standard memory allocation.

How MODeL Works: A Two-Pronged Approach

MODeL treats memory management as a mathematical optimization problem, formulating tensor scheduling and memory allocation as a joint Integer Linear Program (ILP). It tackles the problem from two angles:

1. Optimizing Tensor Lifetimes (Operator Reordering)

First, the algorithm analyzes the model's dataflow graph to find a more memory-efficient sequence of operations. By prioritizing operations that quickly free up large chunks of data, MODeL minimizes the "peak resident set"—the absolute maximum amount of data that must exist in memory at any single moment.

Example of how changing the execution order of a dataflow graph reduces peak memory usage from 60Mb to 45Mb.

As shown above, simply changing the order in which independent operations are executed can drastically reduce peak memory usage. In practice, MODeL's reordering can reduce peak memory by up to 38% on its own.

2. Optimizing Tensor Locations (Memory Packing)

Second, MODeL solves the dynamic storage allocation problem by planning exactly where each tensor should reside in the memory address space. Instead of allocating on the fly, it pre-plans the layout so that tensors fit together perfectly over time, completely eliminating fragmentation.

To make this computationally feasible for massive models with thousands of nodes, the authors introduce several scaling techniques. By using ASAP/ALAP (As Soon/Late As Possible) analysis to bound the search space, an off-the-shelf ILP solver can find an optimal memory plan in just seconds.

Key Results and Insights

The researchers evaluated MODeL across a diverse suite of popular neural network architectures spanning a decade of AI evolution, including Computer Vision (AlexNet, ResNet, ViT), NLP (BERT, XLM-R), and Video Understanding (ResNet3D).

  • Significant Memory Savings: On average, MODeL reduced peak memory usage by 31.4% (batch size 1) and 32.8% (batch size 32). In the best-case scenario with XLM-R, savings reached an impressive 68%.
  • Zero Fragmentation: While PyTorch's default allocator suffered from up to 21.3% fragmentation at larger batch sizes, MODeL's pre-planned address generation fully eliminated fragmentation across all tested models.
  • Fast Optimization: Unlike previous academic attempts at using ILP for memory management, MODeL is highly practical. The optimization is a one-time cost incurred before training begins, taking a median time of less than 7 seconds. Even the largest models tested took only about 18 seconds to solve.
  • Orthogonality: MODeL does not replace existing memory-saving techniques; it complements them. Practitioners can use MODeL to secure a "free" 30% reduction and then apply rematerialization or quantization on top for even greater savings.

Limitations and Future Work

While highly effective, MODeL has a few constraints based on its current design:

  • Static Graphs: The algorithm requires the neural network to be representable as a static dataflow graph. It currently struggles with models featuring highly dynamic control flow (e.g., complex branching based on input data).
  • Known Tensor Sizes: MODeL needs to know the size of every tensor ahead of time. For tasks with variable-length inputs, users must optimize for the "worst-case" maximum length.
  • Single Memory Space: The current formulation assumes a single contiguous memory pool. Extending this ILP approach to multi-GPU setups or heterogeneous memory systems (like CXL) remains a promising direction for future research.

MODeL is available as an open-source project at https://github.com/facebookresearch/model_opt.

Summary updated

BibTeX
@inproceedings{steiner2023model,
  author = {Benoit Steiner and Mostafa Elhoushi and Jacob Kahn and James Hegarty},
  title = {{MODeL: Memory Optimizations for Deep Learning}},
  booktitle = {International Conference on Machine Learning},
  year = {2023},
  url = {https://openreview.net/forum?id=9v29agPZkj},
}
Figure