Coop: Memory is not a Commodity

Jianhao Zhang, Shihan Ma, Peihong Liu, Jinhui Yuan

Introduction

Training massive deep neural networks (DNNs) requires immense GPU memory. To fit huge models on hardware with limited memory budgets, frameworks often rely on tensor rematerialization (also known as activation checkpointing). This technique temporarily evicts intermediate tensors from memory during the forward pass and recomputes them later as needed during backpropagation.

However, current dynamic rematerialization methods suffer from a critical flaw: they treat all free memory as identical, leading to severe memory fragmentation. This paper introduces Coop, a system that co-optimizes memory allocation and eviction to keep free memory contiguous, drastically reducing fragmentation, compute overhead, and search latency.

The Fragmentation Problem

Existing dynamic methods, such as Dynamic Tensor Rematerialization (DTR), make a flawed assumption: they treat memory as a fungible commodity. They assume that freeing 100MB of memory across five scattered 20MB chunks is just as useful as freeing one contiguous 100MB chunk.

In reality, deep learning memory allocators require contiguous memory blocks to allocate new tensors. Evicting scattered tensors leads to severe memory fragmentation. When the system needs to allocate a new tensor, scattered free blocks are useless. This forces the system to evict even more tensors than necessary just to clear a large enough contiguous block, which skyrockets the recomputation cost and slows down training.

Comparison of memory allocation and eviction between DTR and Coop

The Coop Solution

To address this, the authors propose Coop, a framework that co-optimizes tensor allocation and rematerialization. By controlling where tensors are placed initially, the system sets itself up for cheaper, contiguous evictions later. Coop introduces three main techniques to guarantee contiguous memory and minimize recomputation costs:

Overview of Coop's techniques compared to standard DTR
  1. Sliding Window Algorithm: Instead of blindly searching for and evicting the "cheapest" scattered tensors one by one, Coop slides a window across the memory pool to find a contiguous sequence of tensors to evict. This guarantees the freed memory forms a single usable block and operates efficiently in $O(N)$ time.
  2. Cheap Tensor Partitioning: Coop actively organizes the memory layout during forward propagation. It groups "cheap" tensors (those with a low compute-to-memory ratio, like ReLUs) at one end of the memory pool and "expensive" tensors (like convolutions or matrix multiplications) at the other. This makes it much easier for the sliding window to find a contiguous block of cheap tensors to evict later.
  3. Recomputable In-place: Standard rematerialization breaks in-place operations (like x.relu_()) by forcing a copy-on-write. This scatters memory and creates fragmentation landmines. Coop introduces a "functional-but-in-place" paradigm that reuses the exact same memory address for the output, keeping unevictable parameters from fragmenting the memory pool.

Key Results and Insights

The authors benchmarked Coop's performance across eight representative DNN architectures with varying memory profiles, including BERT Large, GPT-3 Style (2.7B parameters), U-Net, ResNet-50, BiLSTM, Inception V3, Swin-Transformer (Swin-T), and SPOS.

  • Compute Overhead: Coop significantly reduces the extra time spent recomputing tensors. For example, on BERT Large at a 50% memory budget, Coop adds only 11% compute overhead, compared to 29% for DTE and 41% for DTR.
  • Memory Savings: Coop reduces the minimum memory budget required to train models by up to 25% compared to DTR, and saves up to 60% memory usage on Inception V3.
  • Search Latency: Because it uses a single $O(N)$ sliding window pass rather than repeatedly searching for individual tensors to evict, Coop's search latency is up to 10x faster (and much more consistent) than baselines.
  • Fragmentation: Coop keeps the memory fragmentation rate below 5% across all tested DNNs and memory budgets. In contrast, DTR and DTE suffer from massive fragmentation spikes that frequently lead to Out-Of-Memory (OOM) errors.

Limitations and Future Work

While highly effective, Coop currently requires its own custom memory pool and cannot share CUDA's built-in stream-ordered memory allocator. This limits its ability to easily share memory with other concurrent programs on the same GPU. Additionally, because Coop makes fast, on-the-fly (online) decisions, its eviction choices might not perfectly match the absolute mathematical optimum that slow, offline solvers can find over several hours or days.

Integrating these co-optimization concepts directly into the default allocators of major frameworks like PyTorch or TensorFlow represents a clear and promising next step for the field.

Summary updated

BibTeX
@inproceedings{zhang2023coop,
  author = {Jianhao Zhang and Shihan Ma and Peihong Liu and Jinhui Yuan},
  title = {{Coop: Memory is not a Commodity}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2023},
  url = {https://openreview.net/forum?id=gmmXyAq8TI},
}
Figure