Randomized Automatic Differentiation

Deniz Oktay, Nick McGreivy, Joshua Aduol, Alex Beatson, Ryan P Adams

In the world of deep learning, practitioners are accustomed to a strict trade-off: training a larger model or using a larger batch size requires proportionally more GPU memory. This limitation is largely driven by Reverse-Mode Automatic Differentiation (AD)—the algorithmic engine behind backpropagation. Standard AD requires storing every intermediate activation from the forward pass to correctly calculate gradients during the backward pass.

However, this introduces a compelling irony: we go to great lengths to compute these "exact" gradients to numerical precision, only to immediately feed them into Stochastic Gradient Descent (SGD), an optimizer that intentionally adds noise by using minibatches.

In their ICLR 2021 paper, Randomized Automatic Differentiation (RAD), Deniz Oktay and colleagues from Princeton University ask a provocative question: Why spend precious memory on exact gradients when we are just going to perform stochastic optimization anyway?

The Memory Bottleneck in Modern AI

The primary bottleneck in scaling modern machine learning is memory. To compute a gradient, standard AD tools (like PyTorch or JAX) must "remember" the state of the network at every layer. For massive models, long sequences in RNNs, or complex scientific simulations, this memory requirement quickly outstrips hardware limits.

Existing solutions, such as gradient checkpointing, save memory by deleting intermediate results and re-computing them during the backward pass. However, this trades memory for computation time. RAD proposes a fundamentally different path: instead of re-computing exactly, we compute an unbiased estimate of the gradient. If the estimate is "noisy" but correct on average, SGD should still converge—and we can save a massive amount of memory in the process.

Methods: Randomizing the Derivative

RAD is a framework that trades gradient precision for memory efficiency by using unbiased stochastic estimators within the differentiation process itself. The authors introduce two primary ways to randomize this process:

Computational graphs illustrating path sampling
  1. Path Sampling: AD can be viewed as summing over all possible paths from the input to the output in a "Linearized Computational Graph." RAD samples a subset of these paths. If you scale the sampled paths correctly, the result is an unbiased estimate of the full gradient.
  2. Random Matrix Injection: For vectorized operations (like matrix multiplications in neural networks), the authors insert random projection matrices into the chain rule. Specifically, for a given layer's output, they might only store a small, random fraction of the activations (e.g., 10%).

The "secret sauce" of RAD is ensuring these estimates remain unbiased. The authors prove mathematically that by carefully choosing how to sample and scale these values, the expected value of their randomized gradient is exactly equal to the true gradient.

Key Insights and The Variance-Memory Trade-off

  • Minibatching is a Subset of RAD: The authors point out that standard minibatch SGD is actually a form of "path sampling" where you sample paths corresponding to specific data points. RAD simply takes this concept and applies it inside the network's internal layers.
  • The Variance-Memory Trade-off: Practitioners can choose to store 50%, 10%, or even 1% of their activations. Less storage means more "noise" (variance) in the gradient, but a significantly smaller memory footprint.
  • Beating the "Small Batch" Trap: Under a strict memory limit, standard practice dictates using a tiny minibatch. The authors show that it is often better to use a larger minibatch with RAD (noisy gradients per sample) than a tiny minibatch with exact AD. The variance reduction gained from the larger batch size outweighs the internal noise introduced by RAD.
  • Controlling Exploding Variance: Naive sampling can lead to variance that grows exponentially with network depth. To mitigate this, RAD leverages the specific structure of neural networks, sampling only the "bottleneck" Jacobians (the activations) rather than every operation.

Empirical Results

The authors evaluated RAD across several domains to prove its versatility, including standard image classification (MNIST, CIFAR-10), sequential data (Sequential-MNIST), and scientific computing.

  • Memory Savings in Vision: For a ConvNet trained on CIFAR-10, RAD reduced the memory required for activations from 23.08 MB to 3.28 MB (at a 10% sampling rate) while still converging effectively.
  • Scientific Computing: In a simulated Linear Reaction-Diffusion PDE representing a fission reactor, RAD achieved a staggering 99% memory reduction with almost no slowdown in convergence.
  • Efficiency: Across multiple tests, when restricted to a fixed memory budget, RAD reached a lower training loss in fewer iterations than standard SGD utilizing a reduced batch size.

Limitations and Future Work

While theoretically sound, RAD faces practical hurdles. Current deep learning frameworks and hardware (like GPUs) are highly optimized for dense, deterministic operations. Implementing the sparse sampling required for RAD efficiently in CUDA/C++ remains a challenge to realize actual "wall-clock" speedups.

Furthermore, some operations, like Softmax, lack "compact" Jacobians that are easy to sparsify using these methods. Introducing more noise might also require careful hyperparameter tuning, such as adjusting learning rates or relying on adaptive optimizers like Adam to handle the increased variance.

Ultimately, RAD presents a compelling paradigm shift. By integrating RAD with techniques like the adjoint method for PDEs, or by developing specialized hardware kernels for "activation sampling," future systems could make stochastic differentiation as fast as it is memory-efficient.

Summary updated

BibTeX
@inproceedings{oktay2021randomized,
  author = {Deniz Oktay and Nick McGreivy and Joshua Aduol and Alex Beatson and Ryan P Adams},
  title = {{Randomized Automatic Differentiation}},
  booktitle = {International Conference on Learning Representations},
  year = {2021},
  url = {https://openreview.net/forum?id=xpx9zj7CUlY},
}
Figure