Dynamic Tensor Rematerialization

Marisa Kirisame, Steven Lyubomirsky, Altan Haan, Jennifer Brennan, Mike He, Jared Roesch, Tianqi Chen, Zachary Tatlock

Introduction

Dynamic Tensor Rematerialization (DTR) is a greedy online algorithm that automatically manages GPU memory during deep learning training by evicting and recomputing intermediate tensors on the fly. It matches the theoretical efficiency of complex static planning while providing out-of-the-box support for dynamic models (like TreeLSTMs) with minimal developer effort.

The Memory Wall in Deep Learning

As deep learning models grow in size and complexity, they frequently hit the "memory wall"—the physical limit of GPU VRAM. During training, the majority of this memory is consumed by "activations," which are intermediate results from the forward pass that must be stored to compute gradients during the backward pass.

To fit larger models or batches, researchers use checkpointing (or rematerialization): they discard some activations and recompute them only when needed. However, existing techniques are mostly static. They require an offline planning phase—often using expensive optimization solvers—that assumes the model’s computation graph is fixed. This approach breaks down for modern dynamic models that use data-dependent control flow (like loops or recursion) and places a heavy manual burden on researchers to "unroll" their models or annotate checkpoints.

Dynamic Tensor Rematerialization (DTR)

The authors introduce DTR, which treats GPU memory like a cache for tensors. Instead of planning ahead, DTR makes decisions at runtime.

DTR Execution Trace and Pseudocode

The system operates through several key mechanisms:

  1. Interception: DTR sits as a thin layer between the deep learning framework (like PyTorch) and the hardware. It intercepts every tensor allocation and operator call.
  2. Greedy Eviction: When a new allocation exceeds the memory budget, DTR heuristically selects "resident" tensors to evict (free from memory) until enough space is cleared. It retains the "metadata" (how the tensor was made) so it can be recovered later.
  3. Recursive Rematerialization: When an operator needs an evicted tensor, DTR "rematerializes" it by re-running the operation that originally produced it. If the inputs to that operation were also evicted, DTR recursively recomputes them.
  4. The Heuristic ($h_{DTR}$): To decide what to evict, DTR uses a scoring function: $$h(t) = \frac{\text{recomputation cost}}{\text{memory size} \cdot \text{staleness}}$$ It favors evicting tensors that are cheap to recompute, large in size, and haven't been used recently.

  5. Union-Find Optimization: To account for "chains" of recomputations without massive runtime overhead, the authors developed a fast approximation using a union-find data structure to track "evicted neighborhoods."

Key Insights

  • Static Planning is Optional: The most surprising insight is that a simple, greedy online algorithm can match the performance of "optimal" static planners. The authors prove that DTR can train an $N$-layer linear network using only $\Omega(\sqrt{N})$ memory with $\mathcal{O}(N)$ operations—the same asymptotic bound as the best static methods.
  • Memory as a Cache: By viewing GPU memory as a cache for intermediate results, DTR decouples memory management from model logic. This allows it to support dynamic models and higher-order gradients (like those in GANs) without any special code from the user.
  • Staleness Matters: Unlike standard caching (which often uses Least Recently Used), DTR finds that combining staleness with recomputation cost is vital to prevent "thrashing," where the system spends all its time recomputing the same few tensors.

Experimental Results

The paper evaluates DTR across a diverse suite of model architectures, including static models (InceptionV4, ResNet-1202, DenseNet-121, U-Net) and dynamic models (LSTM, TreeLSTM, Unrolled GAN).

Compute Overhead vs. Memory Ratio across Models
  • Memory Savings: In simulations, DTR enabled training with 30% to 50% less memory than standard execution with minimal computational overhead.
  • Performance Parity: DTR's performance closely tracked state-of-the-art static optimizers, despite having zero knowledge of the future computation graph.
  • Scaling Up: Using the PyTorch prototype, the authors successfully trained a ResNet-1202 with a batch size of 140 on a single GPU that normally could only fit a batch size of 64—a 2.2x increase in capacity.
  • Dynamic Flexibility: DTR successfully handled the TreeLSTM and Unrolled GAN models, which are notoriously difficult for static checkpointing tools to optimize.

Limitations and Future Work

  • Runtime Overhead: While the logic is simple, searching for eviction candidates in a pool of thousands of tensors adds overhead. The authors suggest using more advanced data structures (like priority queues) or random sampling to mitigate this.
  • Timing Variance: DTR relies on measuring operator execution times. On some hardware, these times can be noisy, leading to sub-optimal eviction choices.
  • Future Directions: The authors suggest integrating DTR with tensor swapping (moving tensors to CPU RAM instead of just deleting them) and using machine learning to help the heuristic "learn" the best eviction patterns from previous training batches.

Summary updated

BibTeX
@inproceedings{kirisame2021dynamic,
  author = {Marisa Kirisame and Steven Lyubomirsky and Altan Haan and Jennifer Brennan and Mike He and Jared Roesch and Tianqi Chen and Zachary Tatlock},
  title = {{Dynamic Tensor Rematerialization}},
  booktitle = {International Conference on Learning Representations},
  year = {2021},
  url = {https://openreview.net/forum?id=Vfs_2RnOD0H},
}
Figure