Moccasin: Efficient Tensor Rematerialization for Neural Networks
The deployment and training of neural networks on edge computing devices pose
many challenges. The low memory nature of edge devices is often one of the
biggest limiting factors encountered in the deployment of large neural network
models. Tensor rematerialization or recompute is a way to address high memory
requirements for neural network training and inference. In this paper we
consider the problem of execution time minimization of compute graphs subject
to a memory budget. In particular, we develop a new constraint programming
formulation called \textsc{Moccasin} with only $O(n)$ integer variables, where
$n$ is the number of nodes in the compute graph. This is a significant
improvement over the works in the recent literature that propose formulations
with $O(n^2)$ Boolean variables. We present numerical studies that show that
our approach is up to an order of magnitude faster than recent work especially
for large-scale graphs.
Burak Bartan, Haoming Li, Harris Teague, Christopher Lott, Bistra Dilkina
# Moccasin: Efficient Tensor Rematerialization for Neural Networks

###### Abstract

The deployment and training of neural networks on edge computing devices pose many challenges. The low memory nature of edge devices is often one of the biggest limiting factors encountered in the deployment of large neural network models. Tensor rematerialization or recompute is a way to address high memory requirements for neural network training and inference. In this paper we consider the problem of execution time minimization of compute graphs subject to a memory budget. In particular, we develop a new constraint programming formulation called Moccasin with only \(O(n)\) integer variables, where \(n\) is the number of nodes in the compute graph. This is a significant improvement over the works in the recent literature that propose formulations with \(O(n^{2})\) Boolean variables. We present numerical studies that show that our approach is up to an order of magnitude faster than recent work especially for large-scale graphs.

Machine Learning, Neural Networks, Machine Learning, Deep Learning, Neural Networks

## 1 Introduction

The need for efficient deep neural network (DNN) computations for both training and inference continues to grow. Most compute architectures for DNNs make use of a limited amount of fast, _local memory_ in conjunction with a much larger amount of _global memory_ that is significantly slower to access. For example, accelerators for mobile devices today will dedicate several MB of local memory (per core) for storage of intermediate output tensors before resorting to the devices' main memory; training of large DNNs are done primarily on GPU's local GDDR memory before offloading tensors to DDR memory on the motherboard. However, there are many DNNs in practice for which the local memory is not enough to store all the required intermediate outputs.

Effective optimization of the local memory footprint during the computation can make a difference in meeting latency targets for computations since global memory accesses can be too slow (Sze et al., 2017, Sec. V). In addition, the trend toward on-device training will further stress the limited resources due to the need to store intermediate data for gradient back-propagation (tinyML Foundation, 2022).

There are several techniques for DNN computation sequencing that can be used to manage the local memory footprint, and a well-developed toolchain for this purpose is likely to employ multiple methods working together. For example, _operation sequencing_ is a technique studied in (Gagrani et al., 2022) and its references. Our paper focuses on another technique called _rematerialization_. When input data is needed for a compute operation, it can be read from local memory, or alternatively be recomputed (rematerialized). In some cases the extra compute can prove to be advantageous relative to occupying valuable local memory with the data until it is ready to be used, with the latter leaving a larger memory footprint. Deciding what operations to rematerialize (or not) in order to optimize latency and memory-use is a PSPACE-complete problem (Gilbert et al., 1979). Further, we often need a method that can tackle large computation graphs with acceptable "compile time", thus need an approach that scales well with graph size.

While there is a history of rematerialization research for managing register use by compilers (Kubota, 1998; Briggs

Figure 1: Percentage of total duration increase against solve time in seconds for our proposed method (Moccasin) and Checkmate(Jain et al., 2020) on a real-world graph with \(n=442\) nodes and \(m=1247\) edges. The memory budget is set to \(80\%\) of the peak memory for the initial topological order without rematerialization. The time until the first data point in each curve is spent on presolve.
et al., 1992), more recently, these techniques have been further developed and studied for use in DNNs, where the size of computation graphs and the amount of data movement can be extremely large (Jain et al., 2020; Patil et al., 2022; Schuler et al., 2022). These works formulate a mixed integer linear program (MILP) with a Boolean variable for each use of a compute node's output - either read from memory or rematerialized. We have found that this formulation has limitations when attempting to scale to large graphs due to the need for \(O(n^{2})\) Boolean variables (where \(n\) is the number of nodes in the computation graph). While linear relaxation followed by rounding is an approximation that is leveraged in these works, we have found that these rounded solutions can be far from optimal, limiting the applicability of this approach.

The general rematerialization problem is stated here, and will be made more explicit in Section 2. Given a directed acyclic graph (DAG), \(G=(V,E)\), with \(|V|=n\) and \(|E|=m\), let nodes \(v\in V\) represent compute operations and directed edges \((u,v)\in E\) represent data dependencies such that the output tensors of all the nodes \(\{w:(w,v)\in E\}\) (predecessors of \(v\)) are required to be present in local memory before computing \(v\). Let \(seq(G)\) be a _rematerialization sequence_ of nodes (ordered list) that contains each node at least once. The rematerialization problem statement is then as follows:

Memory-constrained Computation Graph Sequencing with Rematerialization \[\underset{seq(G)}{\text{minimize}}\quad\text{total execution duration}\] subject to \[-\text{seq}(G)\text{ meets the data dependencies of }G\] \[-\text{peak memory footprint of }seq(G)\] \[\qquad\leq\text{local memory capacity }M\]

This definition is intentionally high-level and lacking some details that will be provided later. Since our later formulations do not directly optimize the sequence but rather optimize other variables from which memory footprints and the final sequence are computed, we skip further description of such details in this section.

Our work introduces a new optimization model formulation that defines the problem variables as _retention intervals_, allowing use of only \(O(n)\) integer variables. We then leverage constrained programming (CP), allowing complex data dependency and cumulative memory threshold constraints to be enforced easily and efficiently during optimization. We further show that our proposed approach demonstrates significant scaling improvements relative to the MILP formulation of Checkmate(Jain et al., 2020). Figure 1 provides a numerical comparison of the two methods.

### Related Work

The problem of rematerialization has its roots in compiler optimization (Colombet et al., 2015; Lozano and Schulte, 2019; Lozano et al., 2019) and automatic differentiation with checkpointing (Kubota, 1998; Griewank and Walther, 2008; 2000). Recently, there is a stream of works on rematerialization in machine learning, motivated by the need to train large DNN on GPU with limited memory capacity (Kumar et al., 2019; Kusumoto et al., 2019; Beaumont et al., 2021; Chen et al., 2016; Kirisame et al., 2021; Mostafa, 2022; Huang et al., 2019); most relevant to us are the works based on combinatorial optimization (Jain et al., 2020; Patil et al., 2022; Schuler et al., 2022).

The scope of our work is most closely aligned with that of (Jain et al., 2020), where they develop a mixed integer linear program (MILP) for duration minimization under memory constraints referred to as Checkmate. The formulation of Checkmate defines Boolean matrix variables \(R,S,F\) for node/tensor recomputation, local memory storage, and deallocation, respectively, resulting in \(O(n^{2})\) Boolean variables. While the paper demonstrates the capability of rematerialization to meet memory limits while minimizing duration, the paper does not sufficiently address the problem complexity and scaling to larger graphs. The authors do mention this issue and offer a method of linear relaxation of the Boolean variables followed by 2-stage rounding to accelerate finding a solution. However, our experiments show that for many graphs, this rounding approach produces results that are far from optimal, and often the rounded solution does not meet the memory threshold and is thus infeasible (see Section 3).

As a consequence of formulating the problem using matrices of variables where the columns represent specific nodes (permutations are not supported), in Checkmate all solutions are constrained to follow an input sequence of the graph nodes without rematerialization. We call this the "input topological order". This input must be a valid topological order for solutions that meet the node precedence requirements to be possible. The question then arises, "what input topological order should be used?" Indeed this constraint significantly reduces the space of potential valid rematerialization sequences so can be valuable to limit algorithm complexity. However, as shown in (Gagrani et al., 2022), there can be a wide variability of peak memory footprint for different sequences (without rematerialization).

One of the attractive features of our new formulation is that it does not require an input topological order. However, when studying the graphs used in (Jain et al., 2020), we found no variations in the peak memory footprint across 50 randomly generated topological orders for each graph (without rematerialization). To compare directly with prior methods, we add the input topological order constraint (see Section 2.3) and use this for all of our experiments. Remov
ing the input topological order constraint and/or studying the impact of different input topological orders on solution quality and solve time are an interesting topic for future research.

A follow-on to Checkmate(Patil et al., 2022) recognizes that memory optimization may combine rematerialization with paging - strategically reading and writing intermediate data to external memory. While these external memory accesses are slow, they still may be required if the available local memory is too small to fit any rematerialization solution of practical total duration. This work, however, still suffers from a scaling issue as it inherits the \(O(n^{2})\) Boolean variable formulation. An extension of our formulation to incorporate paging would be valuable future research. Another extension is (Schuler et al., 2022) which builds on the Checkmate formulation to allow making rematerialization decisions across two heterogeneous cores. This extension also suffers from the same fundamental scaling issue.

In (Kumar et al., 2019), a novel method is introduced for finding a valid rematerialization sequence (one that meets the required memory threshold) by performing divide-and-conquer using tree decomposition. This work focuses on proving an attractive complexity for generating such a sequence. However, it is of limited use in practice since it does not contain any explicit mechanism for minimizing total duration of the final sequence produced by the algorithm.

The potential for reducing peak local memory footprint using rematerialization depends on the computation graph topology. For example, a line graph (string of nodes connected by single edges) offers no potential for improvement since the local memory footprint at each node does not depend on past deallocations. In contrast, a simple U-net typically allows significant opportunities for footprint savings. In general, we observe that networks with "long skip connections" are the topologies that exhibit more potential for rematerialization gains. DNN training computation graphs have a "U-net-like" structure since they have forward and backward paths with edges that cross between the two to support gradient computation in the backward path. (Jain et al., 2020) only experiments with training graphs. In contrast, we study rematerialization on both training and inference graphs, and find compelling rematerialization gains also for inference graphs, particularly ones with complex interconnect topology.

### Contributions

* _Retention intervals formulation of rematerialization:_ We introduce Moccasin, a new formulation with significantly better scaling of solution complexity to large graphs. We demonstrate that this formulation can be solved effectively using _constraint programming_ (CP).
* _Constrained number of allowed rematerializations:_ We introduce parameter \(C_{v}\) that defines the maximum number of times a node \(v\) can be computed in the final sequence. We demonstrate empirically that this complexity reduction retains solution quality even for very small values of \(C_{v}\).
* _Compare and contrast approaches:_ We provide comparison of solution speed for Checkmate vs Moccasin and demonstrate equivalence of solutions.
* _Impact of memory limit:_ Prior works make simple assumptions on local memory limit in their evaluation. We show the impact of a range of local memory limits on solution speed and final solution value.

## 2 Retention Intervals Formulation

The main building block of our formulation is the concept of _output retention intervals_ which, as we will show, simplifies the problem formulation greatly. For each node in the computation graph, we will define intervals that indicate the retention of its output in local memory. More precisely, each rematerialization of a node will have its own interval. Here, with a misuse of terminology, we use the term rematerialization for the first time that a node is computed as well. We will assume a node \(v\in V\) is allowed to be rematerialized up to \(C_{v}\) times, hence \(C_{v}\) intervals will be defined for node \(v\). The parameter \(C_{v}\) could be considered a hyperparameter. We will show via numerical experiments that picking \(C_{v}\) to be as small as \(2\) for all of the nodes is often good enough in practice and is without loss of optimality (see Section 3).

We define the retention intervals for each node \(v\in V\) by its "start" and "end" times

\[s_{v}^{i},e_{v}^{i}\in\mathcal{D},\ \forall i\in\left\{1,\ldots,C_{v}\right\},\]

where the domain \(\mathcal{D}\) is a set of integers of size \(O(n)\) that will be defined explicitly in the subsequent subsections. A memory block of size \(m_{v}\) is allocated at the start and deallocated at the end of the interval.

The concept of time in our formulation is defined through _compute events_. This is one of the key components of our proposed formulation. To better illustrate this point, consider Figure 3, which depicts our formulation for the 4-node example graph in Figure 2. In this example, we have set \(C_{v}=2\) for all nodes \(v\). Figure 3 illustrates the formulation

Figure 2: Example graph with 4 nodes.
for this graph where the vertical axis corresponds to different nodes and the horizontal axis is the time partitioned into events. The formulation as visualized in Figure 3 shows two intervals for node 1 and one interval for the other nodes. The reason for this is that the optimal solution for this example case requires only one active interval for nodes 2, 3, 4 while requiring two active intervals for node 1. We note that the starting and ending positions of the intervals in Figure 3 are obtained by solving the optimization problem. These are not known ahead of time, but rather modeled as variables in the optimization problem. Furthermore, this figure depicts circles as visual representation of potential node execution events. For example, in Figure 3, node 1 is computed during event 1 and its output is kept in memory until event 3. The second interval for node 1 starts in event 7 indicating that node 1 is recomputed during event 7. Observe that event 1 has a memory usage of \(m_{1}\), while event 7 has a memory usage of \(m_{1}+m_{3}\), as the output of node 3 is retained in memory when node 1 is recomputed.

The total duration of the rematerialization sequence is a weighted sum over the events in which a node is being executed, where the weights are simply the actual durations of the nodes (in seconds or processor cycles). Such events are marked with a filled circle in Figure 3. We say that the _peak memory footprint_ of a solution is the maximum memory usage among all events. The cyan colored arrows in Figure 3 represent the memory usage of node execution events. The peak memory footprint of the solution in the example is 3, assuming each node outputs a unit-size tensor, realized at event 10. Observe that events marked with empty circles do not contribute to either the duration or the peak memory footprint of the solution.

The primary reason why we work with this event-based definition for time is to keep the domain of the variables \(s^{i}_{v},e^{i}_{v}\) at a small size. More concretely, the CP-SAT solver (Perron and Furnon, 2022), which we use to solve numerically our proposed optimization problem, accepts only integer variables as it implements Lazy Clause Generation (Ohrimenko et al., 2009), known to be effective for constrained scheduling problems (Schutt et al., 2013). If we instead set the time domain to be seconds (which would require quantization) or processor cycles, then the corresponding CP formulation would require variable domains of larger sizes. The domain size of the variables in the formulation has a direct impact on the solver speed especially for CP solvers.

Note that for each interval, the first time slot (shown using a filled circle in Figure 3) is dedicated to the computation of node \(v\) while the rest of the interval indicates the retention of the output of node \(v\). It follows that intervals may not start at the same time due to our system model where nodes are executed sequentially, i.e. there is no parallel computing. The empty circles in Figure 3 indicate that no interval starts at that particular time, i.e., no computation occurs. The decision of which circles will be filled or empty is a by-product of the optimization problem and not known ahead of time. Furthermore, it is important to note that a memory block of size \(m_{v}\) is allocated during the entirety of the interval. Also, observe that since the first event of an interval indicates the compute of that node, this is when the predecessors of that node must be available in memory.

### Optimization Problem Formulation

In addition to the start and end times of the intervals, we define the Boolean variables \(a^{i}_{v}\) to model whether the \(i\)'th interval of node \(v\) is active or inactive. This grants us the flexibility of not requiring a node \(v\) to be rematerialized exactly \(C_{v}\) times. If inactive (i.e. \(a^{i}_{v}=0\)), the corresponding interval does not contribute to the sum in the objective as well as the sums in the memory and precedence constraints. We assume that the duration \(w_{v}\) and output size \(m_{v}\) for each node are known. Using the retention intervals as the foundation of our formulation, we state the rematerialization problem as an optimization problem as follows:

\[\underset{s,e,a}{\text{minimize}} \sum_{v,i}w_{v}a^{i}_{v}\] (1) subject to \[s^{i}_{v}\leq e^{i}_{v},\;\forall v,i \tag{2}\] \[e^{i}_{v}\leq s^{i+1}_{v},\;\forall v,\forall i<C_{v}\] (3) \[\sum_{v,i\,:\,s^{i}_{v}\leq t\leq e^{i}_{v}}m_{v}a^{i}_{v}\leq M, \;\forall t\in\mathcal{D}\] (4) \[\forall(u,v)\in E,\,\forall i\in\{i:a^{i}_{v}=1\},\,\exists j\;\text {such that}\] \[a^{j}_{u}=1,s^{j}_{u}+1\leq s^{i}_{v}\leq e^{j}_{u}\] (5) \[s^{i}_{v}\neq s^{j}_{u},\;\forall v,u\in\{v,u:v\neq u\},\,\forall i,j\] (6) \[a^{1}_{v}=1,\,\forall v\] (7) \[s^{i}_{v},e^{i}_{v}\in\mathcal{D},\,a^{i}_{v}\in\{0,1\},\, \forall v,i\,. \tag{8}\]

The objective (1) is the total duration. Constraints (2) enforce that the end time of each interval comes after its start time. Constraints (3) ensure that intervals of the same node

Figure 3: Visualization of the retention intervals formulation.
do not overlap. The memory budget constraints are given in (4), where the parameter \(t\) represents time. The precedence constraints (5) enforce that there exists an overlapping and active interval for all predecessors of \(v\) at all start times \(s^{i}_{v}\) of \(v\). Observe that the memory and precedence constraints are nonlinear constraints. We describe in detail how the _cumulative constraint_ and the _reservoir constraint_ from constraint programming are used to model these nonlinear constraints in the next subsection. The constraints in (7) are intended to make the first retention interval for every node active. The constraints (6) ensure that the starting times of the intervals are all different, i.e. compute events do not overlap.

The domain of the variables \(s^{i}_{v},e^{i}_{v}\) is as follows

\[\mathcal{D}=\{1,2,\ldots,\sum_{v}C_{v}\}\,, \tag{9}\]

where \(\sum_{v}C_{v}\) is equal to the number of intervals, which is upper bounded by \(|\mathcal{D}|\leq n\max_{v}C_{v}\). Observe that we may not have more than \(\sum_{v}C_{v}\) filled circles in Figure 3.

### Memory and Precedence Constraints using CP

**Memory constraints:** The set of constraints in (4) enforces that the resource usage never exceeds the local memory budget \(M\) at any time \(t\). Note that the constraint (4) is not linear in the variables \(s^{i}_{v},e^{i}_{v}\). This type of inequality can be modeled using the CP constraint cumulative.

The cyan colored arrows in Figure 3 are intended to visually represent how we model the memory constraint. Each vertical arrow can be viewed as a separate inequality constraint. In particular, the sum of output sizes \(m_{v}\) for each interval an arrow intersects is less than or equal to \(M\). Recall that it is not needed to have an arrow for each circle in Figure 3 since the memory use could not increase during the empty circles. Hence, we only need to consider the filled circles. However, we do not know which circles are to be filled ahead of time. This is readily addressed by the cumulative modeling which considers only the beginning of intervals for memory calculation.

We have used the function AddCumulative from CP-SAT (Perron and Furnon, 2022) for the memory constraints. This function requires intervals, demands, and capacity as input arguments. Intervals are the retention intervals from our formulation defined by the start and end variables \(s^{i}_{v},e^{i}_{v}\) and considered only when \(a^{i}_{v}=1\). The demand for each interval is the output size \(m_{v}\) for the corresponding node \(v\). Finally, the capacity is simply the memory budget \(M\). The pseudo-code representation of this constraint is as follows:

\[\texttt{cumulative}(\{s^{i}_{v},e^{i}_{v},a^{i}_{v},m_{v}\}_{v\in V,i\in\{1..C_{v}\}},M)\,.\]

**Precedence constraints:** We recall that prior to the execution of each compute task, the outputs of all of its predecessors must be available in memory. This could be written as the constraints in (5). Note that these are nonlinear constraints in the variables. We employ the reservoir constraint from constraint programming to model the data dependencies. We view each predecessor as a resource whose level needs be maintained such that it does not go below \(0\) while its successor is being executed.

Let us define the resource level change events \(f(\cdot)\) for each edge \((u,v)\in E\) and \(i\in\{1,\ldots,C_{v}\}\):

\[f(s^{i}_{v})=-1\] \[f(s^{i}_{v}+1)=1\] \[f(s^{1}_{u})=1,f(s^{2}_{u})=1,\ldots,f(s^{C_{u}}_{u})=1\] \[f(e^{1}_{u})=-1,f(e^{2}_{u})=-1,\ldots,f(e^{C_{u}}_{u})=-1\,. \tag{10}\]

The level change function \(f(\cdot)\) returns \(0\) at every other point in time except for those in (10). The function AddReservoirConstraintWithActive from CP-SAT takes the following input parameters: times, demands, actives, minimum level. Times and demands are as defined in (10). Actives are the Boolean variables \(a^{i}_{v}\) and the minimum level is set to \(0\). This constraint ensures that for node \(v\), one of the intervals for each of its predecessors \(u\) overlaps with the starting time of the node \(v\).

Finally, the nonlinear constraints (6) could be addressed using the alldifferent constraint from CP.

### Complexity Reduction by Enforcing an Initial Topological Ordering

The proposed framework offers the flexibility of allowing solutions that are not limited to a predetermined topological ordering. However, one could enforce an input topological ordering to reduce the size of the search space and in turn reduce the solve time. We expand upon this point in this subsection.

We start by considering a new domain for the integer variables \(s^{i}_{v},e^{i}_{v}\):

\[\mathcal{D}=\left\{1,2,\ldots,\frac{n(n+1)}{2}\right\}, \tag{11}\]

where the domain size is now \(O(n^{2})\) rather than \(O(n)\). We now introduce the concept of _stages_ into our formulation, which has been previously employed in (Jain et al., 2020). The \(j\)'th stage contains \(j\) events; this is depicted in Figure 4. Given a topological ordering of the nodes, the first stage enforces the computation of the first node in the order. In subsequent stages, the \(j\)'th node in the input topological order is enforced to be computed in the last event of stage \(j\), while the previous events in stage \(j\) allow nodes \(1,\ldots,(j-1)\) to be computed. In particular, we enforce that node \(j\) could be computed only in event \(j\) of a stage. Since this
restricts the starting times of each interval to specific events, we no longer need to explicitly include the constraint (6).

Note that the start time for the first retention interval for each node is no longer a variable but a fixed value. More precisely, for node \(v\), the value of \(s^{1}_{v}\) is equal to \(j(j+1)/2\), where \(j\) is the index of node \(v\) in the input topological ordering.

### Optimization in Two Phases

Our approach consists of two phases. In Phase 1, we solve a variant of the optimization problem in (1) - (8) to find a memory feasible solution. This solution is then used as initialization for Phase 2, which is the optimization problem stated in (1) - (8). The problem in Phase 1 has the objective

\[\underset{s,e,a,M_{var}}{\text{minimize}}\quad\max(M_{var},M) \tag{12}\]

where we introduce the variable \(M_{var}\in\mathbb{R}\) for the peak memory footprint. We modify the memory constraint in (4) as follows for Phase 1:

\[\sum_{v,i\,:\,s^{i}_{v}\leq t\leq e^{i}_{v}}m_{v}a^{i}_{v}\leq M_{var},\,\forall t \in\mathcal{D}\,. \tag{13}\]

The goal of Phase 1 is to arrive at an intermediate solution, whose peak memory footprint is below the local memory target \(M\). We address this by considering the maximum of the peak memory variable \(M_{var}\) and memory budget \(M\) as the objective. This objective could be linearized by introducing an auxiliary variable \(\tau\in\mathbb{R}\) and then considering: minimize \(\tau\) subject to \(\tau\geq M_{var}\) and \(\tau\geq M\). The other constraints of the optimization problem are unchanged.

Note that any topological ordering of the graph \(G\) provides a trivial feasible solution to the problem in Phase 1 since it does not have a hard memory constraint. For the rematerialization problem itself, however, there is no easy solution that could be constructed and provided as an initial solution. Phase 1 is intended to fill this gap.

### Overview of Formulations

Table 1 provides a complexity comparison for our approach and the approach of (Jain et al., 2020). When comparing the number of constraints for different formulations, it is important to keep in mind that the constraints in the MILP are all linear constraints while the constraints of the CP could be linear or nonlinear.

_Remark 2.1_.: The optimization problem in (1) - (8) is specified using integer variables \(s^{i}_{v}\) and \(e^{i}_{v}\) while the discrete variables of the MILP in (Jain et al., 2020) are all Boolean. The fact that our formulation is stated using integer variables as opposed to Boolean variables is only for convenience. For a more direct comparison of variable counts, it is straightforward to rewrite this optimization problem by representing the integer variables as Boolean sequences, in which case the number of Boolean decision variables will be \(O(Cn\log n)\) (taking \(C=\max_{v}C_{v}\)) as opposed to \(O(n^{2})\) Boolean variables in the Checkmate formulation.

Note that the size of search space in Table 1 for Moccasin is dominated by the term \(n^{Cn}\). For the version of Moccasin with Boolean decision variables, the search space size would scale with \(2^{Cn\log n}\), which is the same as \(n^{Cn}\). This is smaller than the size of the search space for Checkmate, which is \(2^{n^{2}+nm}\).

## 3 Numerical Results

### Graphs for Evaluation

The graphs we use for evaluation of the method presented in this paper come from several sources. Additional descriptions can be found in Appendix A.2.

**Checkmate Graphs:** We use graphs from the checkmate repository (Jain, 2020). They represent the single-batch training computation graphs for a selected set of neural networks. Use of these graphs allows us to compare directly to results presented in that work.

**Random Layered Graphs:** We leverage the synthetically constructed random layered graphs introduced in (Gagrani et al., 2022, Appendix A) as examples of graphs with complex interconnect topology.

**Real-world Graphs:** While our synthetic and standard, public-domain graphs are valuable for experimentation, we also see value in presenting results obtained from neural computation graphs used for commercial development of artificial intelligence hardware and software products. We sample a set of representative graphs that have diverse architectures and size.

Figure 4: Illustration of events grouped into stages.
### Experimental Setup

We use the open source CP-SAT solver from Google ORTools (Perron and Furnon, 2022) to solve the CP. For the comparisons against Checkmate, we have used the implementation provided in the codebase for (Jain et al., 2020), which utilizes Gurobi (Gurobi Optimization, LLC, 2023) and CVXPY (Diamond and Boyd, 2016; Agrawal et al., 2018). We have run all of the numerical experiments on a 16-CPU core workstation with a 32 GB of RAM. Furthermore, we note that once a solution to the optimization problem is obtained, the corresponding sequence of tasks can be executed on any hardware, CPU or GPU. We note that this is due to the generality of our approach where we do not make any assumptions on the computing unit that will be used to execute the computations.

### Experimental Results

Figure 5 shows the solve progress plots for 4 random layered graphs \(G_{1},\ldots,G_{4}\) of varied sizes and 4 different local memory budget values for each graph. The curves of Moccasin are all shifted to the right by the amount of time spent in Phase 1 for fair comparison. We have set the time limit to \(30\) minutes for \(G_{1},G_{2},G_{3}\) and \(1\) hour for \(G_{4}\).

For \(G_{1}\), the solve times are in the order of a few seconds and we observe that Moccasin is faster to obtain similar objective values as Checkmate. For \(G_{2}\), which has \(n=250\) nodes, when the memory budget is tight, we see that Checkmate fails to find a feasible solution within the given time limit of \(30\) minutes. For the highest memory budget value, it finds the optimal solution in 10 minutes while Moccasin finishes in a few seconds. For graphs \(G_{3}\) and \(G_{4}\), which have \(n=500\) and \(n=1000\) nodes respectively, Checkmate times out with no solution even if we consider a higher time limit of 3 hours while Moccasin converges to a good solution (i.e. low total duration increase) under less than an hour. In addition to the solve time, the memory required for optimization also becomes a critical factor for large graphs. In fact, for \(G_{3}\) and \(G_{4}\), Checkmate exits with an out-of-memory error. The results in Figure 5 are best understood by contrasting the number of discrete variables in Checkmate, which grows quadratically in \(n\) while it grows \(n\log(n)\) in Moccasin (see Table 1).

In all of the experiments, we have set \(C_{v}=2\) for all \(v\in V\), which we specified in the plot legends by \(C=2\). We have used the version of the formulation in Section 2.3 where we enforce an input topological ordering, which we have randomly generated.

Table 2 provides numerical results for a range of different computation graphs. In Table 2 we have selected the memory budget values for each graph to be the \(80\%\) and \(90\%\) of the initial peak memory without rematerialization. In addition to the methods of Checkmate and Moccasin, we include results for the rounding algorithm proposed in (Jain et al., 2020) under the "LP+rounding" column of the table. This method consists of relaxing the MILP into a linear program (LP) and then rounding the solution (see (Jain et al., 2020) for further details on this algorithm). Note that solution produced by the rounding algorithm is not guaranteed to satisfy the memory budget constraint. This could be seen in Table 2 where in most cases the peak memory for the relaxation and rounding approach is higher than the memory budget \(M\).

Table 2 shows that the random layered graphs and real-world graphs are the most challenging ones among the graph set. The solve times for these graphs are higher than the CM graphs, which is consistent with the fact that they have higher edge densities and more complex edge connectivities.

It is critical to highlight that Moccasin is able to scale to graphs with few hundreds of nodes and a few thousands of edges. This is while achieving a few percents of total duration increase, which is essential to performance.

## 4 Conclusion

Rematerialization is a valuable technique that allows to save peak local memory footprint at the expense of longer total duration of computation. However, finding a rematerialization sequence that minimizes total duration while meeting a prescribed local memory limit is not easy - especially for graphs with complex interconnections. Previous research (Jain et al., 2020) demonstrates a MILP formulation that can address this optimization problem, however our experiments show that that approach does not scale well as the graph size, and the complexity of graph topology grow.

We introduce Moccasin, a new formulation that expresses the decision variables as _retention intervals_ specified by the start and end event indices of each node's output tensor. We further manage complexity using the hyper-parameter

\begin{table}
\begin{tabular}{||c c c c c||} \hline Formulation & \# Bool. vars & \# integer vars & Size of search space & \# constraints \\ \hline \hline Checkmate - MILP & \(O(n^{2}+nm)\) & - & \(O(2^{n^{2}+nm})\) & \(O(n^{2}+nm)\) \\ \hline Moccasin - CP & \(O(Cn)\) & \(O(Cn)\) (domain size = \(O(n)\)) & \(O(2^{Cn}+n^{Cn})\) & \(O(Cm)\) \\ \hline \end{tabular}
\end{table}
Table 1: Overview of formulation complexities for Checkmate and our proposed method.
which limits the maximum number of retention intervals for each node. Our experimental results include Checkmate graphs (training graphs with simple interconnect topology), synthetically generated random layered graphs (that model inference graphs with complex topology), and real-world inference graphs in active use commercially. We demonstrate that \(\textsc{Moccasin}\) provides the same optimization result as Checkmate with up to an order-of-magnitude less solve time for mid-sized graphs (100-250 nodes). It also enables us to find solutions for larger graphs (up to 1000 nodes) and for more challenging peak local memory limits, cases where the MILP formulation of Checkmate failed to return any solution within the time limit.

Finally, rematerialization is seen as one component among a set of tools for managing memory use during execution of complex computation graphs arising in deep learning applications. Joint optimization of this with other methods such as sequencing, scheduling for parallel compute, and paging to global memory are viewed as valuable topics for further research.
