R ANDOMIZED A UTOMATIC D IFFERENTIATION
A BSTRACT
The successes of deep learning , variational inference and many other ﬁelds have been aided by specialized implementations of reverse-mode automatic differentiation ( AD ) to compute gradients of mega-dimensional objectives.
The AD techniques underlying these tools were designed to compute exact gradients to numerical precision, but modern machine learning models are almost always trained with stochastic gradient descent.
Why spend computation and memory on exact (minibatch) gradients only to use them for stochastic optimization?
We develop a general framework and approach for randomized automatic differentiation (RAD) which can allow unbiased gradient estimates to be computed with reduced memory in return for variance.
We examine limitations of the general approach and argue that we must leverage problem speciﬁc structure to realize beneﬁts.
We develop
RAD techniques for a variety of simple neural network architectures and show that for a ﬁxed memory budget, RAD converges in fewer iterations than using a small batch size for feedforward networks and in a similar number for recurrent networks.
We also show that RAD can be applied to scientiﬁc computing and use it to develop a low-memory stochastic gradient method for optimizing the control parameters of a linear reaction-diffusion PDE representing a ﬁssion reactor
I NTRODUCTION
Deep neural networks have taken center stage as a powerful way to construct and train massivelyparametric machine learning (ML) models for supervised, unsupervised, and reinforcement learning tasks.
There are many reasons for the resurgence of neural networks—large data sets, GPU numerical computing, technical insights into overparameterization, and more—but one major factor has been the development of tools for automatic differentiation (AD) of deep architectures.
Tools like PyTorch and
TensorFlow provide a computational substrate for rapidly exploring a wide variety of differentiable architectures without performing tedious and error-prone gradient derivations.
The ﬂexibility of these tools has enabled a revolution in AI research, but the underlying ideas for reverse-mode AD go back decades.
While tools like PyTorch and TensorFlow have received huge dividends from a half-century of AD research, they are also burdened by the baggage of design decisions made in a different computational landscape.
The research on AD that led to these ubiquitous deep learning frameworks is focused on the computation of Jacobians that are exact up to numerical precision.
However, in modern workﬂows these Jacobians are used for stochastic optimization.
We ask:
Why spend resources on exact gradients when we’re going to use stochastic optimization?
This question is motivated by the surprising realization over the past decade that deep neural network training can be performed almost entirely with ﬁrst-order stochastic optimization.
In fact, empirical evidence supports the hypothesis that the regularizing effect of gradient noise assists model generalization (Keskar et al., 2017; Smith & Le, 2018; Hochreiter & Schmidhuber, 1997).
Stochastic gradient descent variants such as AdaGrad (Duchi et al., 2011) and Adam (Kingma & Ba, 2015) form the core of almost all successful optimization techniques for these models, using small subsets of the data to form the noisy gradient estimates.
from math import sin, exp def f(x1, x2): a =
[Equation]
(x1) b = sin( [Equation] ) c = [Equation]
= [Equation] ∗ c return a ∗ d (a) Differentiable Python function x x a exp(x1) b sin(x2)
c b * x d a * c f a * d (b) Primal graph x x a b c d f exp(x1) cos(x2) x b c a d a (c) Linearized graph x x a b c d f exp(x1) cos(x2) x b c a d a (d) Bauer paths
The goals and assumptions of automatic differentiation as performed in classical and modern systems are mismatched with those required by stochastic optimization. Traditional AD computes the derivative or Jacobian of a function accurately to numerical precision. This accuracy is required for many problems in applied mathematics which AD has served, e.g., solving systems of differential equations. But in stochastic optimization we can make do with inaccurate gradients, as long as our estimator is unbiased and has reasonable variance.
We ask the same question that motivates mini-batch SGD: why compute an exact gradient if we can get noisy estimates cheaply?
By thinking of this question in the context of AD, we can go beyond mini-batch SGD to more general schemes for developing cheap gradient estimators: in this paper, we focus on developing gradient estimators with low memory cost.
Although previous research has investigated approximations in the forward or reverse pass of neural networks to reduce computational requirements, here we replace deterministic
AD with randomized automatic differentiation (RAD), trading off of computation for variance inside
AD routines when imprecise gradient estimates are tolerable, while retaining unbiasedness.
A UTOMATIC D IFFERENTIATION
Automatic (or algorithmic ) differentiation is a family of techniques for taking a program that computes a differentiable function f : R n → R m , and producing another program that computes the associated derivatives; most often the Jacobian: J [ f ] = f (cid:48) : R n → R m × n .
(For a comprehensive treatment of AD, see Griewank & Walther (2008); for an ML-focused review see Baydin et al.
In most machine learning applications, f is a loss function that produces a scalar output, i.e., m = 1 , for which the gradient with respect to parameters is desired.
AD techniques are contrasted with the method of ﬁnite differences , which approximates derivatives numerically using a small but non-zero step size, and also distinguished from symbolic differentiation in which a mathematical expression is processed using standard rules to produce another mathematical expression, although Elliott (2018) argues that the distinction is simply whether or not it is the compiler that manipulates the symbols.
There are a variety of approaches to AD: source-code transformation (e.g., Bischof et al.
Hascoet & Pascual (2013); van Merrienboer et al.
, execution tracing (e.g., Walther & Griewank (2009); Maclaurin et al.)
, manipulation of explicit computational graphs (e.g., Abadi et al.
Bergstra et al.
(2010)), and category-theoretic transformations (Elliott, 2018).
AD implementations exist for many different host languages, although they vary in the extent to which they take advantage of native programming patterns, control ﬂow, and language features.
Regardless of whether it is constructed at compile-time, run-time, or via an embedded domain-speciﬁc language, all AD approaches can be understood as manipulating the linearized computational graph (LCG) to collapse out intermediate variables.
Figure 1 shows the LCG for a simple example.
These computational graphs are always directed acyclic graphs (DAGs) with vertices as variables.
Let the outputs of f be y j , the inputs θ i , and the intermediates z l .
AD can be framed as the computation of a partial derivative as a sum over all paths through the LCG DAG (Bauer, 1974):
[Equation]
where [ i → j ] indexes paths from vertex i to vertex j and ( k, l ) ∈
[ i → j ] denotes the set of edges in that path.
See Figure 1d for an illustration.
Although general, this naïve sum over paths does not take advantage of the structure of the problem and so, as in other kinds of graph computations, dynamic programming (DP) provides a better approach.
DP collapses substructures of the graph until it becomes bipartite and the remaining edges from inputs to outputs represent exactly the entries of the
Jacobian matrix.
This is referred to as the Jacobian accumulation problem (Naumann, 2004) and there are a variety of ways to manipulate the graph, including vertex, edge, and face elimination (Griewank & Naumann, 2002).
Forward-mode AD and reverse-mode AD (backpropagation) are special cases of more general dynamic programming strategies to perform this summation; determination of the optimal accumulation schedule is unfortunately NP-complete (Naumann, 2008).
While the above formulation in which each variable is a scalar can represent any computational graph, it can lead to structures that are difﬁcult to reason about.
Often we prefer to manipulate vectors and matrices, and we can instead let each intermediate z l represent a d l dimensional vector.
In this case, ∂z l / ∂z k ∈ R d l × d k represents the intermediate Jacobian of the operation z
k
→ z l .
Note that
Equation 1 now expresses the Jacobian of f as a sum over chained matrix products.
R ANDOMIZING A UTOMATIC D IFFERENTIATION
We introduce techniques that could be used to decrease the resource requirements of AD when used for stochastic optimization.
We focus on functions with a scalar output where we are interested in the gradient of the output with respect to some parameters, J θ [ f ] .
Reverse-mode AD efﬁciently calculates J θ [ f ] , but requires the full linearized computational graph to either be stored during the forward pass, or to be recomputed during the backward pass using intermediate variables recorded during the forward pass.
For large computational graphs this could provide a large memory burden.
The most common technique for reducing the memory requirements of AD is gradient checkpointing (Griewank & Walther, 2000; Chen et al., 2016), which saves memory by adding extra forward pass computations.
Checkpointing is effective when the number of "layers" in a computation graph is much larger than the memory required at each layer.
We take a different approach; we instead aim to save memory by increasing gradient variance, without extra forward computation.
Our main idea is to consider an unbiased estimator ˆ J θ [ f ] such that E ˆ J θ [ f ] =
J θ [ f ] which allows us to save memory required for reverse-mode AD.
Our approach is to determine a sparse (but random) linearized computational graph during the forward pass such that reverse-mode AD applied on the sparse graph yields an unbiased estimate of the true gradient.
Note that the original computational graph is used for the forward pass, and randomization is used to determine a LCG to use for the backward pass in place of the original computation graph.
We may then decrease memory costs by storing the sparse LCG directly or storing intermediate variables required to compute the sparse LCG.
In this section we provide general recipes for randomizing AD by sparsifying the LCG.
In sections 4 and 5 we apply these recipes to develop speciﬁc algorithms for neural networks and linear PDEs which achieve concrete memory savings.
3. P ATH S AMPLING
Observe that in Bauer’s formula each Jacobian entry is expressed as a sum over paths in the LCG.
A simple strategy is to sample paths uniformly at random from the computation graph, and form a Monte Carlo estimate of Equation 1.
Naïvely this could take multiple passes through the graph.
However, multiple paths can be sampled without signiﬁcant computation overhead by performing a topological sort of the vertices and iterating through vertices, sampling multiple outgoing edges for each.
We provide a proof and detailed algorithm in the appendix.
Dynamic programming methods such as reverse-mode automatic differentiation can then be applied to the sparsiﬁed LCG.
3. R ANDOM M ATRIX I NJECTION
In computation graphs consisting of vector operations, the vectorized computation graph is a more compact representation.
We introduce an alternative view on sampling paths in this case.
A single path in the vectorized computation graph represents many paths in the underlying scalar computation graph.
As an example, Figure 2c is a vector representation for Figure 2b.
For this example,
[Equation]
[Equation] where A, B, C are vectors with entries a i , b i , c i , ∂C / ∂B , ∂B / ∂A are 3 × 3 Jacobian matrices for the intermediate operations, ∂y / ∂C is 1 × 3 , and ∂A / ∂θ is 3 × 1 . θ aaabbbcccy (a) Independent Paths Graph θ aaabbbcccy (b) Fully Interleaved Graph θ
A R
B R
C Ry (c) Vector graph for (b).
We now note that the contribution of the path p
= θ → a 1 → b 2 → c 2 → y to the gradient is,
[Equation]
(3) where P i =
e i e Ti (outer product of standard basis vectors).
Sampling from { P 1 , P 2 , P 3 } and right multiplying a Jacobian is equivalent to sampling the paths passing through a vertex in the scalar graph.
In general, if we have transition B → C in a vectorized computational graph, where B ∈ R d , C ∈ R m , we can insert a random matrix P =
d / k (cid:80) ks
=1 P s where each P s is sampled uniformly from { P 1 , P 2 , . .
. , P d } .
With this construction, E P = I
d , so
[Equation]
If we have a matrix chain product, we can use the fact that the expectation of a product of independent random variables is equal to the product of their expectations, so drawing independent random matrices P B , P C would give
[Equation]
Right multiplication by P may be achieved by sampling the intermediate Jacobian: one does not need to actually assemble and multiply the two matrices.
For clarity we adopt the notation S P [ ∂C / ∂B ] = ∂C / ∂B P .
This is sampling (with replacement) k out of the d vertices represented by B , and only considering paths that pass from those vertices.
The important properties of P that enable memory savings with an unbiased approximation are
[Equation]
We could therefore consider other matrices with the same properties.
In our additional experiments in the appendix, we also let R be a random projection matrix of independent Rademacher random variables, a construction common in compressed sensing and randomized dimensionality reduction.
In vectorized computational graphs, we can imagine a two-level sampling scheme.
We can both sample paths from the computational graph where each vertex on the path corresponds to a vector.
We can also sample within each vector path, with sampling performed via matrix injection as above.
In many situations the full intermediate Jacobian for a vector operation is unreasonable to store.
Consider the operation B → C where B, C ∈ R d .
The Jacobian is d × d .
Thankfully many common operations are element-wise, leading to a diagonal Jacobian that can be stored as a d -vector.
Another common operation is matrix-vector products.
Consider Ab = c , ∂c / ∂b =
A .
Although A has many more entries than c or b , in many applications A is either a parameter to be optimized or is easily recomputed.
Therefore in our implementations, we do not directly construct and sparsify the
Jacobians.
We instead sparsify the input vectors or the compact version of the Jacobian in a way that has the same effect.
Unfortunately, there are some practical operations such as softmax that do not have a compactly-representable Jacobian and for which this is not possible.
3. V ARIANCE
The variance incurred by path sampling and random matrix injection will depend on the structure of the LCG.
We present two extremes in Figure 2.
In Figure 2a, each path is independent and there are a small number of paths.
If we sample a ﬁxed fraction of all paths, variance will be constant in the depth of the graph.
In contrast, in Figure 2b, the paths overlap, and the number of paths increases exponentially with depth.
Sampling a ﬁxed fraction of all paths would require almost all edges in the graph, and sampling a ﬁxed fraction of vertices at each layer (using random matrix injection, as an example) would lead to exponentially increasing variance with depth.
It is thus difﬁcult to apply sampling schemes without knowledge of the underlying graph.
Indeed, our initial efforts to apply random matrix injection schemes to neural network graphs resulted in variance exponential with depth of the network, which prevented stochastic optimization from converging.
We develop tailored sampling strategies for computation graphs corresponding to problems of common interest, exploiting properties of these graphs to avoid the exploding variance problem.
C ASE S TUDY : N EURAL N ETWORKS
We consider neural networks composed of fully connected layers, convolution layers, ReLU nonlinearities, and pooling layers.
We take advantage of the important property that many of the intermediate
Jacobians can be compactly stored, and the memory required during reverse-mode is often bottlenecked by a few operations.
We draw a vectorized computational graph for a typical simple neural network in ﬁgure 3.
Although the diagram depicts a dataset of size of 3 , mini-batch size of size 1 , and 2 hidden layers, we assume the dataset size is N .
Our analysis is valid for any number of hidden layers, and also recurrent networks.
We are interested in the gradients ∂y / ∂W 1 and ∂y / ∂W 2 .
4. M INIBATCH SGD AS R ANDOMIZED AD
At ﬁrst look, the diagram has a very similar pattern to that of 2a, so that path sampling would be a good ﬁt.
Indeed, we could sample B < N paths from W 1 to y , and also B paths from W 2 to y .
Each path corresponds to processing a different mini-batch element, and the computations are independent.
In empirical risk minimization, the ﬁnal loss function is an average of the loss over data points.
Therefore, the intermediate partials ∂y / ∂h 2 ,x for each data point x will be independent of the other data points.
As a result, if the same paths are chosen in path sampling for W 1 and W 2 , and if we are only interested in the stochastic gradient (and not the full function evaluation), the computation graph only needs to be evaluated for the data points corresponding to the sampled paths.
This exactly corresponds to mini-batching.
The paths are visually depicted in Figure 3b.
4. A LTERNATIVE SGD SCHEMES WITH R ANDOMIZED AD
W 1 R D × Hxxx
R D h 1 ,h 1 ,h 1 ,
R Ha 1 ,a 1 ,a 1 ,
R H
W 2 R H
×
Hh 2 ,h 2 ,h 2 ,
R Hy R × × ×
ReLU
ReLU
ReLU × × × (a) Neural network computational graph
W 1 R D × Hxxx
R D h 1 ,h 1 ,h 1 ,
R Ha 1 ,a 1 ,a 1 ,
R H
W 2 R H
×
Hh 2 ,h 2 ,h 2 ,
R Hy R × × ×
ReLU
ReLU
ReLU × × × ( [Figure]
We wish to use our principles to derive a randomization scheme that can be used on top of mini-batch SGD.
We ensure our estimator is unbiased as we randomize by applying random matrix injection independently to various intermediate Jacobians.
Consider a path corresponding to data point 1 .
The contribution to the gradient ∂y / ∂W 1 is ∂y
∂h 2 ,∂h 2 , [Equation]
Using random matrix injection to sample every Jacobian would lead to exploding variance.
Instead, we analyze each term to see which are memory bottlenecks.
∂y / ∂h 2 , 1 is the Jacobian with respect to (typically) the loss.
Memory requirements for this Jacobian are independent of depth of the network.
The dimension of the classiﬁer is usually smaller ( 10 − 1000 ) than the other layers (which can have dimension 10 , 000 or more in convolutional networks).
Therefore, the Jacobian at the output layer is not a memory bottleneck. X W H
A W [Figure] ∂h 2 , 1 / ∂a 1 , 1 is the Jacobian of the hidden layer with respect to the previous layer activation.
This can be constructed from W 2 , which must be stored in memory, with memory cost independent of mini-batch size.
In convnets, due to weight sharing, the effective dimensionality is much smaller than H 1 × H 2 .
In recurrent networks, it is shared across timesteps.
Therefore, these are not a memory bottleneck.
∂a 1 , 1 / ∂h 1 , 1 contains the Jacobian of the ReLU activation function.
This can be compactly stored using 1 -bit per entry, as the gradient can only be 1 or 0 .
Note that this is true for ReLU activations in particular, and not true for general activation functions, although ReLU is widely used in deep learning.
For ReLU activations, these partials are not a memory bottleneck.
∂h 1 , 1 / ∂W 1 contains the memory bottleneck for typical ReLU neural networks.
This is the Jacobian of the hidden layer output with respect to W 1 , which, in a multi-layer perceptron, is equal to x 1 .
For B data points, this is a B × D dimensional matrix.
Accordingly, we choose to sample ∂h 1 , 1 / ∂W 1 , replacing the matrix chain with ∂y∂h 2 , 1 ∂h 2 , 1 ∂a 1 , 1 ∂a 1 , 1 ∂h 1 , 1 S P W 1 (cid:104) ∂h 1 , 1 ∂W 1 (cid:105) .
For an arbitrarily deep NN, this can be generalized: ∂y
[Equation] ∂a d − 1 ,∂a d − 1 ,∂h d − 1 , 1 . . .
∂a 1 ,∂h 1 , 1 S P W(cid:20)
∂h 1 ,∂W(cid:21) , ∂y ∂h
d,∂h d,∂a d − 1 ,∂a d − 1 ,∂h d − 1 , 1 . . .
∂a 2 ,∂h 2 , 1 S P W(cid:20)
∂h 2 ,∂W(cid:21)
This can be interpreted as sampling activations on the backward pass.
This is our proposed alternative
SGD scheme for neural networks: along with sampling data points, we can also sample activations, while maintaining an unbiased approximation to the gradient.
This does not lead to exploding variance, as along any path from a given neural network parameter to the loss, the sampling operation is only applied to a single Jacobian.
Sampling for convolutional networks is visualized in Figure 4.
4. N EURAL N ETWORK E XPERIMENTS
We evaluate our proposed RAD method on two feedforward architectures: a small fully connected network trained on MNIST, and a small convolutional network trained on CIFAR-10.
We also evaluate our method on an RNN trained on Sequential-MNIST.
The exact architectures and the calculations for the associated memory savings from our method are available in the appendix.
In
Figure 5 we include empirical analysis of gradient noise caused by RAD vs mini-batching.
We are mainly interested in the following question:
For a ﬁxed memory budget and ﬁxed number of gradient descent iterations, how quickly does our proposed method optimize the training loss compared to standard SGD with a smaller mini-batch?
Reducing the mini-batch size will also reduce computational costs, while RAD will only reduce memory costs.
Theoretically our method could reduce computational costs slightly, but this is not our focus.
We only consider the memory/gradient variance tradeoff while avoiding adding signiﬁcant overhead on top of vanilla reverse-mode (as is the case for checkpointing).
Results are shown in Figure 6.
Our feedforward network full-memory baseline is trained with a minibatch size of 150 .
For RAD we keep a mini-batch size of 150 , and try 2 different conﬁgurations.
For "same sample", we sample with replacement a 0 . 1 fraction of activations, and the same activations are sampled for each mini-batch element.
For “different sample”, we sample a 0 .
1 fraction of activations, independently for each mini-batch element.
Our "reduced batch" experiment is trained without RAD with a mini-batch size of 20 for CIFAR-10 and 22 for MNIST.
This achieves similar memory budget as RAD with mini-batch size 150.
Details of this calculation and of hyperparameters are in the appendix.
[Figure]
BaselineReducedbatchSameSampleDiﬀerentSample
[Figure]
Fraction of activations
[Table]
[Figure]
For the feedforward networks we tune the learning rate and (cid:96) 2 regularization parameter separately for each gradient estimator on a randomly held out validation set.
We train with the best performing hyperparameters on bootstrapped versions of the full training set to measure variability in training.
Details are in the appendix, including plots for train/test accuracy/loss, and a wider range of fraction of activations sampled.
All feedforward models are trained with Adam.
In the RNN case, we also run baseline, “same sample”, “different sample” and “reduced batch” experiments.
The “reduced batch” experiment used a mini-batch size of 21 , while the others used a mini-batch size of 150 .
The learning rate was ﬁxed at 10 − 4 for all gradient estimators, found via a coarse grid search for the largest learning rate for which optimization did not diverge.
Although we did not tune the learning rate separately for each estimator, we still expect that with a ﬁxed learning rate, the lower variance estimators should perform better.
When sampling, we sample different activations at each time-step.
All recurrent models are trained with SGD without momentum.
C ASE S TUDY : R EACTION -D IFFUSION PDE-C ONSTRAINED O PTIMIZATION
Our second application is motivated by the observation that many scientiﬁc computing problems involve a repeated or iterative computation resulting in a layered computational graph.
We may apply RAD to get a stochastic estimate of the gradient by subsampling paths through the computational graph.
For certain problems, we can leverage problem structure to develop a low-memory stochastic gradient estimator without exploding variance.
To illustrate this possibility we consider the optimization of a linear reaction-diffusion PDE on a square domain with Dirichlet boundary conditions, representing the production and diffusion of neutrons in a ﬁssion reactor (McClarren,
Simulating this process involves solving for a potential φ ( x, y, t ) varying in two spatial coordinates and in time.
The solution obeys the partial differential equation:
[Equation]
We discretize the PDE in time and space and solve on a spatial grid using an explicit update rule φ t +1 =
Mφ t + ∆ t C t
(cid:12) φ t , where M summarizes the discretization of the PDE in space.
The exact form is available in the appendix.
The initial condition is φ 0 = sin ( πx ) sin ( πy ) , with φ = 0 on the boundary of the domain.
The loss function is the time-averaged squared error between φ and a time-dependent target,
L = 1 / T (cid:80) t
||
φ t ( θ ) − φ target t || 22 .
The target is φ target t = φ 0 + 1 / 4 sin ( πt ) sin (2 πx ) sin ( πy ) .
The source C is given by a seven-term Fourier series in x and t , with coefﬁcients given by θ ∈ R 7 , where θ is the control parameter to be optimized.
Full simulation details are provided in the appendix.
The gradient is ∂L∂ θ = (cid:80) Tt =1 ∂L∂ φ t (cid:80) ti =1 (cid:0) (cid:81) t − 1 j = i
∂
φ j +1 ∂ φ j (cid:1) ∂ φ i ∂ C
i − 1 ∂ C
i − 1 ∂ θ .
As the reaction-diffusion PDE is linear and explicit, ∂ φ j +1 / ∂ φ j ∈ R N 2 x × N 2 x is known and independent of φ .
We avoid storing C at each timestep by recomputing C from θ and t .
This permits a low-memory stochastic gradient estimate without exploding variance by sampling from ∂L / ∂ φ t ∈ R N 2 x
and the diagonal matrix ∂ φ i / ∂ C
i − 1 , replacing ∂L∂θ with the unbiased estimator
T (cid:88) t =1 S P φ t (cid:20) ∂L ∂ φ t (cid:21) t (cid:88) i = (cid:0) t − 1 (cid:89) j = i ∂ φ j + ∂ φ j (cid:1) S P φ i −(cid:20) ∂ φ i ∂ C
i −(cid:21) ∂ C i −∂ θ . This estimator can reduce memory by as much as 99% without harming optimization; see Figure 7b.
R ELATED W ORK
[Figure]
Approximating gradients and matrix operations
Much thought has been given to the approximation of general gradients and Jacobians.
We draw inspiration from this literature, although our main objective is designing an unbiased gradient estimator, rather than an approximation with bounded accuracy.
Abdel-Khalik et al.
(2008) accelerate Jacobian accumulation via random projections, in a similar manner to randomized methods for SVD and matrix multiplication.
Choromanski &
Sindhwani (2017) recover Jacobians in cases where
AD is not available by performing a small number of function evaluations with random input perturbations and leveraging known structure of the Jacobian (such as sparsity and symmetry) via compressed sensing.
Other work aims to accelerate neural network training by approximating operations from the forward and/or backward pass.
Sun et al.
(2017) and Wei et al.
(2017) backpropagate sparse gradients, keeping only the top k elements of the adjoint vector.
Adelman & Silberstein (2018) approximate matrix multiplications and convolutions in the forward pass of neural nets nets using a columnrow sampling scheme similar to our subsampling scheme.
Their method also reduces the computational cost of the backwards pass but changes the objective landscape.
Related are invertible and reversible transformations, which remove the need to save intermediate variables on the forward pass, as these can be recomputed on the backward pass.
Maclaurin et al.
(2015) use this idea for hyperparameter optimization, reversing the dynamics of SGD with momentum to avoid the expense of saving model parameters at each training iteration.
Gomez et al.
(2017) introduce a reversible ResNet (He et al., 2016) to avoid storing activations.
Chen et al.
(2018) introduce Neural
ODEs, which also have constant memory cost as a function of depth.
Limited-memory learning and optimization Memory is a major bottleneck for reverse-mode AD, and much work aims to reduce its footprint.
Gradient checkpointing is perhaps the most well known, and has been used for both reverse-mode AD (Griewank & Walther, 2000) with general layerwise computation graphs, and for neural networks (Chen et al., 2016).
In gradient checkpointing, some subset of intermediate variables are saved during function evaluation, and these are used to re-compute downstream variables when required.
Gradient checkpointing achieves sublinear memory cost with the number of layers in the computation graph, at the cost of a constant-factor increase in runtime.
Stochastic Computation Graphs Our work is connected to the literature on stochastic estimation of gradients of expected values, or of the expected outcome of a stochastic computation graph.
The distinguishing feature of this literature (vs. the proposed RAD approach) is that it uses stochastic estimators of an objective value to derive a stochastic gradient estimator, i.e., the forward pass is randomized.
Methods such as REINFORCE (Williams, 1992) optimize an expected return while avoiding enumerating the intractably large space of possible outcomes by providing an unbiased stochastic gradient estimator, i.e., by trading computation for variance.
This is also true of mini-batch
SGD, and methods for training generative models such as contrastive divergence (Hinton, 2002), and stochastic optimization of evidence lower bounds (Kingma & Welling, 2013).
Recent approaches have taken intractable deterministic computation graphs with special structure, i.e. involving loops or the limits of a series of terms, and developed tractable, unbiased, randomized telescoping series-based estimators for the graph’s output, which naturally permit tractable unbiased gradient estimation (Tallec & Ollivier, 2017; Beatson & Adams, 2019;
Chen et al., 2019; Luo et al., 2020).
C ONCLUSION
We present a framework for randomized automatic differentiation.
Using this framework, we construct reduced-memory unbiased estimators for optimization of neural networks and a linear PDE.
Future work could develop RAD formulas for new computation graphs, e g., using randomized rounding to handle arbitrary activation functions and nonlinear transformations, integrating RAD with the adjoint method for PDEs, or exploiting problem-speciﬁc sparsity in the Jacobians of physical simulators.
The randomized view on AD we introduce may be useful beyond memory savings: we hope it could be a useful tool in developing reduced-computation stochastic gradient methods or achieving tractable optimization of intractable computation graphs. A CKNOWLEDGEMENTS
The authors would like to thank Haochen Li for early work on this project.
We would also like to thank Greg Gundersen, Ari Seff, Daniel Greenidge and Alan Chung for helpful comments on the manuscript.
This work is partially supported by NSF IIS-2007278.
A PPENDIX A: N EURAL N ETWORK E XPERIMENTS R ANDOM P ROJECTIONS FOR RAD
As mentioned in Section 3.2 (around Equation 5) of the main paper, we could also use different matrices P that have the properties
[Equation]
In the appendix we report experiments of letting R be a matrix of iid Rademacher random variables, scaled by √ k .
P = RR T deﬁned in this way satisﬁes the properties above.
Note that this would lead to additional computation: The Jacobian or input vector would have to be fully computed, and then multiplied by R and stored.
In the backward pass, it would have to be multiplied by R T .
We report results as the “project” experiment in the full training/test curves in the following sections.
We see that it performs competitively with reducing the mini-batch size.
A RCHITECTURES U SED
We use three different neural network architectures for our experiments: one fully connected feedforward, one convolutional feedforward, and one recurrent.
Our fully-connected architecture consists of:
Input: 784 -dimensional ﬂattened MNIST Image 2.
Linear layer with 300 neurons (+ bias) (+ ReLU) 3.
Linear layer with 300 neurons (+ bias) (+ ReLU) 4. Linear layer with 300 neurons (+ bias) (+ ReLU) 5.
Linear layer with 10 neurons (+ bias) (+ softmax)
Our convolutional architecture consists of:
Input: 3 × 32 ×
32 -dimensional CIFAR-10 Image 2.
5 × 5 convolutional layer with 16 feature maps (+ 2 zero-padding) (+ bias) (+ ReLU) 3.
5 × 5 convolutional layer with 32 feature maps (+ 2 zero-padding) (+ bias) (+ ReLU) 4.
2 × 2 average pool 2 -d 5.
5 × 5 convolutional layer with 32 feature maps (+ 2 zero-padding) (+ bias) (+ ReLU) 6. 5 × 5 convolutional layer with 32 feature maps (+ 2 zero-padding) (+ bias) (+ ReLU) 7.
2 × 2 average pool 2 -d (+ ﬂatten) 8.
Linear layer with 10 neurons (+ bias) (+ softmax)
Our recurrent architecture was taken from Le et al.
and consists of:
Input: A sequence of length 784 of 1-dimensional pixels values of a ﬂattened MNIST image.
A single RNN cell of the form h t
=
ReLU ( W ih
[Equation] ih [Equation] ) where the hidden state ( h t ) dimension is 100 and x t is the 1-dimensional input.
An output linear layer with 10 neurons (+ bias) (+ softmax) that has as input the last hidden state.
C ALCULATION OF MEMORY SAVED FROM RAD
For the baseline models, we assume inputs to the linear layers and convolutional layers are stored in 32 -bits per dimensions.
The ReLU derivatives are then recalculated on the backward pass.
For the RAD models, we assume inputs are sampled or projected to 0 .
1 of their size (rounded up) and stored in 32 -bits per dimension.
Since ReLU derivatives can not exactly be calculated now, we assume they take 1 -bit per dimension (non-reduced dimension) to store.
The input to the softmax layer is not sampled or projected.
In both cases, the average pool and bias gradients does not require saving since the gradient is constant.
For MNIST fully connected, this gives (per mini-batch element memory):
Baseline: (784 + 300 + 300 + 300 + 10) · 32 bits = 6 .
kBytes
RAD 0 .
1 : (79 + 30 + 30 + 30 + 10) · 32 bits + (300 + 300 + 300) · 1 bits = 828 .
5 bytes which leads to approximately 8 x savings per mini-batch element.
For CIFAR-10 convolutional, this gives (per mini-batch element memory):
Baseline: (3 · 32 · 32+16 · 32 · 32+32 · 16 · 16+32 · 16 · 16+32 · 8 · 8+10) · 32 bits = 151 .
kBytes
RAD 0 .
1 : (308+1639+820+820+205+10) · 32 bits +(16384+8192+8192+2048) · 1 bits = 19 .
56 kBytes which leads to approximately 7 . 5 x savings per mini-batch element.
For Sequential-MNIST RNN, this gives (per mini-batch element memory):
Baseline: (784 · (1 + 100) + 100 + 10) · 32 bits = 317 .
176 kBytes
RAD 0 .
1 : (784 · (1 + 10) + 10 + 10) · 32 bits + (784 · 100) · 1 bits = 44 .
376 kBytes which leads to approximately 7 .
15 x savings per mini-batch element.
F EEDFORWARD NETWORK TRAINING DETAILS
We trained the CIFAR-10 models for 100 , 000 gradient descent iterations with a ﬁxed mini-batch size, sampled with replacement from the training set.
We lower the learning rate by 0 .
6 every 10 ,iterations.
We train with the Adam optimizer.
We center the images but do not use data augmentation.
The MNIST models were trained similarly, but for 20 , 000 iterations, with the learning rate lowered by 0 .
6 every 2 , 000 iterations.
We ﬁxed these hyperparameters in the beginning and did not modify them.
We tune the initial learning rate and (cid:96) 2 weight decay parameters for each experiment reported in the main text for the feedforward networks.
For each experiment (project, same sample, different sample, baseline, reduced batch), for both architectures, we generate 20 (weight decay, learning rate) pairs, where each weight decay is from the loguniform distribution over 0 .
0000001 − 0 .
001 and learning rate from loguniform distribution over 0 .
00001 − 0 .
We then randomly hold out a validation dataset of size 5000 from the CIFAR-10 and MNIST training sets and train each pair on the reduced training dataset and evaluate on the validation set.
For each experiment, we select the hyperparameters that give the highest test accuracy.
For each experiment, we train each experiment with the best hyperparameters 5 times on separate bootstrapped resamplings of the full training dataset ( 50 , 000 for CIFAR-10 and 60 , 000 for MNIST), and evaluate on the test dataset ( 10 , 000 for both).
This is to make sure the differences we observe across experiments are not due to variability in training.
In the main text we show 3 randomly selected training curves for each experiment.
Below we show all 5 .
All experiments were run on a single NVIDIA K80 or V100 GPU.
Training times were reported on a
V100.
RNN TRAINING DETAILS
All RNN experiments were trained for 200,000 iterations (mini-batch updates) with a ﬁxed mini-batch size, sampled with replacement from the training set.
We used the full MNIST training set of 60, images whereby the images were centered.
Three repetitions of the same experiment were performed with different seeds.
Hyperparameter tuning was not performed due to time constraints.
The hidden-to-hidden matrix ( W hh ) is initialised with the identity matrix, the input-to-hidden matrix ( W ih ) and hidden-to-output (last hidden layer to softmax input) are initialised with a random matrix where each element is drawn independently from a N (0 , 0 . 001) distribution and the biases ( b ih , b hh ) are initialised with zero.
The model was evaluated on the test set of 10,000 images every 400 iterations and on the entire training set every 4000 iterations.
For the "sample", "different sample", "project" and "different project" experiments different activations/random matrices were sampled at every time-step of the unrolled RNN.
All experiments were run on a single NVIDIA K80 or V100 GPU.
The average running times for each experiment are given in Table 2.
Note that we did not optimise our implementation for speed and so these running times can be reduced signiﬁcantly.
[Table]
I MPLEMENTATION
The code is provided on GitHub 1 .
Note that we did not optimize the code for computational efﬁciency; we only implemented our method as to demonstrate the effect it has on the number of gradient steps to train.
Similarly, we did not implement all of the memory optimizations that we account for in our memory calculations; in particular in our implementation we did not take advantage of storing ReLU derivatives with 1 -bit or the fact that average pooling has a constant derivative.
Although these would have to be implemented in a practical use-case, they are not necessary in this proof of concept.
F ULL TRAINING / TEST CURVES FOR MNIST AND CIFAR-
[Figure]
ProjectReducedbatchBaselineSameSampleDiﬀerentSample
[Figure]
ProjectReducedbatchBaselineSameSampleDiﬀerentSample
[Figure]
ProjectReducedbatchBaselineSameSampleDiﬀerentSample
[Figure]
F ULL TRAINING / TEST CURVES FOR RNN ON S EQUENTIAL -MNIST
[Figure]
WEEP
FRACTIONS SAMPLED FOR AND
[Figure]
A PPENDIX B: R EACTION -D IFFUSION PDE
The reaction-diffusion equation is a linear parabolic partial differential equation.
In ﬁssion reactor analysis, it is called the one-group diffusion equation or one-speed diffusion equation, shown below.
[Equation]
Here φ represents the neutron ﬂux, D is a diffusion coefﬁcient, and Cφ and S are source terms related to the local production or removal of neutron ﬂux.
In this paper, we solve the one-speed diffusion equation in two spatial dimensions on the unit square with the condition that φ = 0 on the boundary.
We assume that D is constant equal to 1 / 4 , C
( x, y, t, θ ) is a function of control parameters θ described below, and S is zero.
We discretize φ on a regular grid in space and time, which motivates the notation φ → φ t .
The grid spacing is ∆ x = 1 / 32 and the timestep is ∆ t = 1 / 4096 .
We simulate from t = 0 to t = 10 .
We use the explicit forward-time, centered-space (FTCS) method to timestep φ .
The timestep is chosen to satisfy the stability criterion, D ∆ t / (∆ x ) 2 ≤ 14 .
In matrix notation, the
FTCS update rule can be written φ t +
=
Mφ t + ∆ t C t
(cid:12) φ t , in index notation it can be written as follows:
[Equation]
The term Cφ in the one-speed diffusion equation relates to the local production or removal of neutrons due to nuclear interactions.
In a real ﬁssion reactor, C is a complicated function of the material properties of the reactor and the heights of the control rods.
We make the simplifying assumption that
C can be described by a 7-term Fourier series in x and t , written below.
Physically, this is equivalent to the assumption that the material properties of the reactor are constant in space and time, and the heights of the control rods are sinusoidally varied in x and t .
φ 0 is initialized so that the reactor begins in a stable state, the other parameters are initialized from a uniform distribution.
[Equation]
The details of the stochastic gradient estimate and optimization are described in the main text.
The
Adam optimizer is used.
Each experiment of 800 optimization iterations runs in about 4 hours on a
GPU.
A PPENDIX C: P ATH S AMPLING A LGORITHM AND A NALYSIS
Here we present an algorithm for path sampling and provide a proof that it leads to an unbiased estimate for the gradient.
The main idea is to sample edges from the set of outgoing edges for each vertex in topological order, and scale appropriately.
Vertices that have no incoming edges sampled can be skipped.
Algorithm 1 RMAD with path sampling 1: Inputs : 2:
G =
( V, E ) - Computational Graph.
d v denotes outdegree, v.succ successor set of vertex v .
3: y - Output vertex 4: Θ = ( θ 1 , θ 2 , . . .
, θ m )
⊂ V - Input vertices 5: k > 0 - Number of samples per vertex 6: Initialization : 7: Q
∀ e ∈ 8: for v in topological order; synchronous with forward computation do 9: if No incoming edge of v has been sampled then 10:
Continue 11: for k times do 12:
Sample i from [ d v ] uniformly.
[Equation]
[Equation] [Equation] [Equation] [Equation]
14: Run 15: Output : ∇ y
The main storage savings from Algorithm 1 will come from Line 9, where we only consider a vertex if it has an incoming edge that has been sampled.
In computational graphs with a large number of independent paths, this will signiﬁcantly reduce memory required, whether we record intermediate variables and recompute the LCG, or store entries of the LCG directly.
To see that path sampling gives an unbiased estimate, we use induction on the vertices in reverse topological order.
For every vertex z , we denote ¯ z
= ∂y∂z
and ˆ z as our approximation for ¯ z .
For our base case, we let ˆ y = dydy
= 1 , so E
ˆ y =
¯ y .
For all other vertices z , we deﬁne
[Equation]
where d z is the out-degree of z , v i is sampled uniformly from the set of successors of z , and I v = v
i is an indicator random variable denoting if v = v i .
We then have
[Equation]
assuming that the randomness over the sampling of outgoing edges is independent of ˆ v , which must be true because our induction is in reverse topological order.
Since by induction we assumed E ˆ v = ¯ v , we have
[Equation]
which completes the proof.