Optimizing Automatic Differentiation with Deep Reinforcement Learning

Jamie Lohoff, Emre Neftci

TL;DR

AlphaGrad leverages Deep Reinforcement Learning—specifically an AlphaZero-based agent—to discover the mathematically optimal sequence for computing exact Jacobians. By framing Automatic Differentiation as a "game" of graph node elimination, the method reduces necessary multiplications by up to 33% compared to state-of-the-art heuristics, translating to significant runtime speedups in JAX.

The Challenge of Optimal Automatic Differentiation

Automatic Differentiation (AD) is the foundational engine behind modern machine learning (via backpropagation) and scientific computing. While most machine learning practitioners are familiar with "reverse-mode" AD, this is just one specific method for traversing a computational graph. For functions that are not "funnel-shaped"—meaning they have many inputs and many outputs, requiring a full Jacobian matrix—standard forward or reverse modes are often highly inefficient.

To find the most efficient sequence of operations to compute a Jacobian, researchers use a framework known as cross-country elimination. However, finding the optimal elimination order is an NP-complete problem.

Visualizing the cross-country elimination process on a computational graph.

Historically, the field has relied on simple greedy heuristics, such as the "minimal Markowitz degree," to guess the best path. The critical gap addressed by this research is that these human-designed heuristics leave significant computational performance on the table, particularly for complex scientific simulations in fluid dynamics, chemistry, or robotics.

Methods: Framing Math as a Game

To solve this, the authors introduce AlphaGrad, which reformulates the AD process as a single-player game called VertexGame.

The AlphaGrad pipeline: from computational graph to RL optimization and final JAX execution.
  1. VertexGame: The "board" state is the current computational graph. An "action" consists of choosing an intermediate vertex to eliminate. Each elimination incurs a cost (the number of multiplications required) and transforms the graph. The ultimate goal is to eliminate all intermediate vertices with the lowest total accumulated cost.
  2. The AlphaZero Agent: The researchers employ an AlphaZero-based RL agent equipped with a Transformer-based policy and value network. This network processes a 3D adjacency tensor representing the graph. By using Monte Carlo Tree Search (MCTS), the agent can "look ahead" to discover non-obvious, highly efficient elimination orders that greedy algorithms miss.
  3. Graphax Interpreter: Theoretical sequences are only useful if they can be executed efficiently. The authors developed Graphax, a specialized AD interpreter built on top of JAX. Graphax executes these custom "cross-country" elimination orders while actively exploiting the internal sparsity of the Jacobians to maximize speed.

Key Insights

  • Exactness is Non-Negotiable: Unlike many optimization techniques that trade accuracy for speed via approximations, AlphaGrad produces the exact Jacobian. The optimization occurs entirely in the path taken to compute the result, not the mathematical result itself.
  • RL Outperforms Greedy Heuristics: The RL agent frequently discovers counter-intuitive elimination orders. Because greedy algorithms only optimize for the immediate next step, they often fall into local optima. AlphaGrad's look-ahead capabilities allow it to bypass these traps, outperforming standard Markowitz degree heuristics.
  • Sparsity is the Secret Sauce: A major advantage of the Graphax implementation is its ability to handle "vector-valued" vertices. By recognizing when a Jacobian is diagonal or exhibits a specific sparsity pattern, Graphax replaces expensive dense matrix multiplications with cheap element-wise operations.

Experimental Results

The researchers evaluated AlphaGrad across a diverse set of domains, including fluid dynamics (RoeFlux), robotics (6-DOF Robot Arm), chemistry/physics (MINPACK collection), finance (Black-Scholes), and machine learning (MLPs and Transformers).

  • Multiplication Reduction: AlphaGrad achieved up to 33% fewer multiplications compared to the best existing heuristics on complex tasks like the 3D RoeFlux and various synthetic random functions.
  • Real-World Speedups: Using the Graphax interpreter, the authors proved these theoretical gains translate to practice. AlphaGrad's elimination orders significantly outperformed JAX’s native forward and reverse modes. For instance, in the 3D RoeFlux task, AlphaGrad was consistently faster across all tested batch sizes.
  • Machine Learning Performance: Even in deep learning contexts—where reverse-mode AD is already heavily optimized—AlphaGrad found more efficient computational paths for MLP and Transformer tasks, though the relative gains were more modest compared to scientific computing workloads.

Limitations and Future Directions

While highly effective, the current iteration of AlphaGrad has a few limitations:

  • Static Graphs Only: The method currently requires a static computational graph. It cannot dynamically handle control flow, such as if statements or loops with variable bounds, within the differentiated code.
  • Retraining Overhead: The agent must be trained (or run through MCTS) for each new function. While this upfront cost is negligible for simulation code run millions of times, it presents a hurdle for rapid, iterative prototyping.
  • Proxy Metrics: The agent optimizes for the total number of multiplications. While a strong proxy for speed, it does not account for hardware-specific realities like memory bandwidth, cache hits, or kernel fusion. Future iterations could potentially train the agent directly on "wall-clock" execution time for true end-to-end hardware optimization.

Summary updated

BibTeX
@inproceedings{lohoff2024optimizing,
  author = {Jamie Lohoff and Emre Neftci},
  title = {{Optimizing Automatic Differentiation with Deep Reinforcement Learning}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=hVmi98a0ki},
}
Figure