Moccasin: Efficient Tensor Rematerialization for Neural Networks

Burak Bartan, Haoming Li, Harris Teague, Christopher Lott, Bistra Dilkina

Abstract

As deep learning models grow in complexity, fitting them onto memory-constrained edge AI accelerators becomes increasingly difficult. Tensor rematerialization—the process of recomputing intermediate tensors rather than storing them—offers a solution to stay within strict memory budgets. However, finding an optimal rematerialization schedule is a PSPACE-complete problem that existing Mixed-Integer Linear Programming (MILP) solvers struggle to scale. We present MOCCASIN, a novel optimization framework that utilizes Constraint Programming (CP) and a "retention interval" formulation to reduce variable complexity from $O(n^2)$ to $O(n)$. MOCCASIN achieves up to a 10x speedup in solve time compared to state-of-the-art methods and scales to neural network graphs with over 1,000 nodes.

Introduction

The rapid advancement of deep learning has led to models with massive memory footprints. While cloud-based GPUs can often accommodate these models, edge devices and specialized AI accelerators frequently rely on small, high-speed local memory (such as SRAM) to maintain energy efficiency and low latency. When the intermediate tensors of a model exceed this local memory budget, the system must either resort to slow external DRAM or fail to execute entirely.

Tensor rematerialization is a strategic approach to this bottleneck. By discarding certain intermediate results and recomputing them only when needed, the peak memory usage of a model can be significantly reduced. The primary challenge lies in determining the optimal schedule: which tensors should be kept, which should be recomputed, and at what time? This optimization problem is notoriously difficult to solve at scale.

The Scalability Bottleneck in Existing Methods

Previous state-of-the-art solutions, such as CHECKMATE, formulated the rematerialization problem using Mixed-Integer Linear Programming (MILP). These formulations typically rely on a large Boolean matrix to track whether a specific tensor is resident in memory at every possible time step. This approach results in $O(n^2)$ variables relative to the number of nodes ($n$) in the computation graph.

As neural networks grow to hundreds or thousands of nodes, the $O(n^2)$ complexity causes MILP solvers to become prohibitively slow or crash due to memory exhaustion on the optimization server itself. Furthermore, approximation methods that attempt to "round" fractional MILP solutions often produce schedules that violate the hard memory constraints of the hardware, rendering them unusable in real-world deployments.

The MOCCASIN Approach

MOCCASIN (Memory Optimization via Constraint Programming) shifts the paradigm from a matrix-based time-step view to a Retention Interval view. Instead of tracking the state of a tensor at every tick of the clock, MOCCASIN defines intervals $[start, end]$ for each period a tensor is computed and held in memory.

Key Innovations:

  1. Interval-Based Formulation: By focusing on the lifespan of a tensor, MOCCASIN reduces the number of integer variables from $O(n^2)$ to $O(n)$. This linear scaling is the primary driver of its efficiency.
  2. Constraint Programming (CP) Primitives: MOCCASIN leverages high-level CP primitives such as cumulative (to ensure total memory usage never exceeds the hardware budget) and reservoir (to manage complex data dependencies). These primitives are naturally suited for resource-constrained scheduling problems.
  3. Two-Phase Optimization: To accelerate the search for an optimal schedule, the solver first identifies any feasible solution that fits within the memory budget (Phase 1). It then uses this feasible point as a baseline to minimize the total execution duration (Phase 2).
  4. Bounded Rematerialization ($C_v$): The authors introduce a hyperparameter, $C_v$, which limits the number of times any single node can be recomputed. Empirical testing shows that setting $C_v=2$ is typically sufficient to find near-optimal solutions while drastically reducing the search space.

Experimental Evaluation

The authors evaluated MOCCASIN across a diverse set of computation graphs, including standard training graphs (ResNet50, VGG-based FCN), synthetic random layered graphs with complex skip connections, and proprietary real-world graphs from commercial AI hardware development ranging from 350 to 700 nodes.

Key Results

  • Speed and Efficiency: For mid-sized graphs (100–250 nodes), MOCCASIN found optimal or near-optimal schedules up to 10 times faster than CHECKMATE.
  • Unprecedented Scalability: MOCCASIN successfully generated schedules for graphs with 1,000 nodes. In these scenarios, MILP-based competitors either timed out after several hours or failed due to memory errors.
  • Performance Under Pressure: In highly restrictive scenarios (e.g., where the memory budget is only 80% of the peak required memory), MOCCASIN consistently found valid schedules where previous approximation techniques failed to produce a feasible result.

Discussion and Future Work

MOCCASIN demonstrates that Constraint Programming is a superior tool for the non-linear, resource-constrained nature of neural network scheduling. The paper highlights that models with long skip connections, such as U-Nets or training graphs with forward/backward passes, see the most significant benefits from this optimization.

Currently, MOCCASIN assumes a sequential execution of nodes. Future work includes extending the framework to support parallel execution on multi-core hardware and integrating "paging" strategies—moving tensors between SRAM and DRAM—to handle even larger models. Additionally, allowing the solver to optimize the base topological ordering of the graph could unlock further memory savings beyond what is possible with a fixed input order.

Summary updated

BibTeX
@inproceedings{bartan2023moccasin,
  author = {Burak Bartan and Haoming Li and Harris Teague and Christopher Lott and Bistra Dilkina},
  title = {{Moccasin: Efficient Tensor Rematerialization for Neural Networks}},
  booktitle = {International Conference on Machine Learning},
  year = {2023},
  url = {https://openreview.net/forum?id=GN9bGEWvkx},
}
Figure